懒惰 Ninject Injection

我使用ninject框架。 在我的代码中,我有一个Lazy对象。 我可以创建一个实例,但是当我调用value属性时,我得到了一个exception。

private Lazy psoriasisReportUserControl; [Inject] public Lazy PsoriasisReportUserControl { get { return psoriasisReportUserControl; } set { psoriasisReportUserControl = value; } } 

我有

lazily-initialized类型没有public,无参数构造函数

exception,因为注入不会将方法注入构造函数中。 我想我必须写一个方法来绑定什么创建一个新实例。

使用Ninject的工厂扩展https://github.com/ninject/ninject.extensions.factory

 Bind(typeof (Lazy)).ToMethod( ctx => new Lazy(() => Kernel.Get())); 

您需要在Lazy上使用默认的公共构造函数:

 public Lazy() {}