Ninject公约仅适用于公共课程吗? [英] Does Ninject Conventions only work for public classes?
问题描述
我开始将Ninject用于我的项目,以自动绑定抽象类的所有子类.绑定很简单-如下:
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());
但是,我发现这不起作用.经过一番试验后,我发现无法正常运行的原因是我的所有实现(和抽象类)都标记为 internal .
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?
推荐答案
在.SelectAllClasses()
之前放置一个.IncludingNonPublicTypes()
,并且绑定也适用于内部类.
Put a .IncludingNonPublicTypes()
before the .SelectAllClasses()
and your bindings will also work for internal classes.
也请参见以下问题:无法获取Ninject.Extensions.工作惯例
我认为这与安全性无关.我猜这是关于设计,也许是关于性能:与其他所有类型相比,仅选择公共类型所需的时间更少.
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.
这篇关于Ninject公约仅适用于公共课程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!