日期:2014-05-20 浏览次数:20953 次
using System;
//声明消费者的开放给提供者使用的功能(定义接口IHelloView类型)
namespace IView {
public interface IHelloView {
string txtHello { get; set; }
event EventHandler RenderText;
}
}
using IView;
using System;
namespace View {
//构造消费者(定义HelloView类型)
public class HelloView : IHelloView {
public HelloView() {}
public string txtHello{get;set;}
public event EventHandler RenderText;
public void SayHello() {
this.LoadAss();
this.RenderText(null, EventArgs.Empty);
}
private void LoadAss() {
//反射调用:带参数的构造函数
object[] _args = new object[] { this };
string _assName = "路径\\Model.dll";
string _className = "Model.HelloModel";
System.Activator.CreateInstanceFrom(_assName, _className, false, 0, null, _args, null, null);
}
}
}
using IView;
using System;
namespace Model {
//定义提供者,实现需求
public class HelloModel {
protected IHelloView HellowView { get; set; }
public HelloModel(IHelloView pView) {
this.HellowView = pView;
this.HellowView.RenderText += Render_RenderText;
}
void Render_RenderText(object sender, EventArgs e) {
this.HellowView.txtHello = "Hello World";
}
}
}
using System;
namespace TestIOC {
class Program {
static void Main(string[] args) {
View.HelloView _testView = new View.HelloView();
_testView.SayHello();
Console.WriteLine(_testView.txtHello);
Console.ReadLine();
}
}
}