Does Ninject Conventions only work for public classes?

久未见 提交于 2019-12-04 05:22:36

问题


I started using Ninject for my project, to automatically bind all subclasses of an abstract class. The binding for this is -- nice and easy -- as follows:

kernel.Bind(x => x.FromThisAssembly()
    .SelectAllClasses().
    .InheritedFrom<AbstractGenerator>()
    .BindBase());

However, I found that this doesn't work. After some experimenting I found that the reason for this not working is that all my implementations (and the abstract class) are marked internal.

I could imagine this to be some security feature, to prevent bindings from leaking internals to the outside. But I can add explicit bindings for these classes. Hence, my question is: Does anybody know whether this is intended behavior? Is there some way to fix this, other than making all my classes public?


回答1:


Put a .IncludingNonPublicTypes() before the .SelectAllClasses() and your bindings will also work for internal classes.

Also see this question: Cannot get Ninject.Extensions.Conventions to work

I don't think it's about security. I guess it's about design and maybe about performance: choosing from only the public types takes less time than choosing from all types.



来源:https://stackoverflow.com/questions/19317545/does-ninject-conventions-only-work-for-public-classes

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