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();
}
}
}
设计时刻的依赖关系:
HelloView和HelloModel依赖IHelloView,
通过实现IHellowView,消费者开放一组属性,方法或者事件让提供者去处理,
从而可以使用任何类型的提供者
ioc 设计
------解决方案--------------------
------解决方案--------------------
没懂是啥意思。
------解决方案--------------------
谢谢你不解释,你省事,我省心。
------解决方案--------------------
出售瓜子、花生、汽水
------解决方案--------------------