Inject attribute doesn't work for field

点点圈 提交于 2019-12-02 02:26:10

问题


Inject attribute is not working for field.

[Inject]
public MyContext context; //Not injected

[Inject]
public MyContext context {get; set;} //Injected

I am using default Ninject settings. Why field is not injected ?


回答1:


Because it is not allowed.

From the Ninject 2 Beta announcement:

Things that were in Ninject 1.x that are not in Ninject 2:

  • Field injection: Ninject 2’s injection is now driven by expression trees, and in .NET 3.5 there is no way to set field values with an expression tree. Since this is a bad practice anyway, I decided to cut it.


来源:https://stackoverflow.com/questions/5532812/inject-attribute-doesnt-work-for-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!