我有一个 WinForms 应用程序,代码如下:
static void Main()
{
IKernel kernel = new StandardKernel(new MyModule());
TestInterface test = kernel.Get<TestInterface>();
}
对于 Module.Load() 事件:
Bind<TestClass>().ToSelf().InSingletonScope();
Bind<TestInterface>().To<TestClass>();
在此刻test
in the Main()
方法是我期待的正确对象。
在稍后的表单中,我使用属性注入:
[Inject]
TestInterface test {get;set;}
加载表单后,尝试使用test
,但它是一个空对象。
想法?
确保在 IKernel 实例上调用 Inject() 并传入表单的实例。这将确保正确注入所有依赖项。例如...
[Inject]
TestInterface Test { get; set; }
private void Form_Load(object sender, EventArgs e)
{
IKernel kernel = ... // Get an instance of IKernel
kernel.Inject(this);
// Your form dependencies have now been injected and the
// Test property is ready to use
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)