懒&LT;&GT; Ninject注入 [英] Lazy<> Ninject Injection
本文介绍了懒&LT;&GT; Ninject注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用ninject框架。在我的code我有一个懒惰的对象。我可以创建一个实例,但是当我打电话value属性我得到一个例外。
私人懒&LT; IPsoriasisReportUserControl&GT; psoriasisReportUserControl;
[注入]
公共懒&LT; IPsoriasisReportUserControl&GT; PsoriasisReportUserControl
{
{返回psoriasisReportUserControl; }
集合{psoriasisReportUserControl =价值; }
}
我
该延迟初始化类型不具有公共,无参数 构造
异常,因为注入不注入方法到构造。我想我必须写一个方法是什么创建一个新的实例绑定。
解决方案
绑定(typeof运算(懒惰&LT; IPsoriasisReportUserControl&GT;))ToMethod(。
CTX =&GT;新的懒惰&LT; IPsoriasisReportUserControl&GT;(()=&GT;
Kernel.Get&其中; IPsoriasisReportUserControl&GT;()));
I use ninject framework. In my code I have a Lazy object. I can create an instance, but when I call the value property I got an exception.
private Lazy<IPsoriasisReportUserControl> psoriasisReportUserControl;
[Inject]
public Lazy<IPsoriasisReportUserControl> PsoriasisReportUserControl
{
get { return psoriasisReportUserControl; }
set { psoriasisReportUserControl = value; }
}
I got
The lazily-initialized type does not have a public, parameterless constructor
exception because the injection does not inject the method into the constructor. I think I have to write a method to the bind what Creates a new instance.
解决方案
Bind(typeof (Lazy<IPsoriasisReportUserControl>)).ToMethod(
ctx => new Lazy<IPsoriasisReportUserControl>(() =>
Kernel.Get<IPsoriasisReportUserControl>()));
这篇关于懒&LT;&GT; Ninject注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文