从 ninject 获取所有具体类型
有没有办法使用 ninject 从绑定中获取具体类型?我不想实例化实现,只是获取类型。
我想做这样的事情
Type[] concreteTypes = Kernel.GetBindings(typeof(IController)).
GetImplementingTypes();
我的Kernel
只是一个StandardKernel
。
我想最终反思这些类型。
Is there a way with ninject to get the concrete types from a binding? I don't want to instantiate the implementations, just get the type.
I want do to do something like this
Type[] concreteTypes = Kernel.GetBindings(typeof(IController)).
GetImplementingTypes();
My Kernel
is just a StandardKernel
.
I want to eventually reflect against those types.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
唯一的方法是使用
kernel.GetAll()
获取所有实例。 Ninject 没有设置绑定映射。当实例通过注入或 get 调用解析时,实现就确定了。这是因为 Ninject 允许条件绑定和隐式绑定。如果您设置了 MVC3 应用程序,则实际上并不需要注册控制器,因为 Ninject 将确定要加载哪个控制器以及要向其中注入什么内容。它通过延迟创建隐式绑定来实现此目的。
最接近的方法是扩展
StandardKernel
并访问受保护的绑定集并查找所有非条件绑定。拥有所有非条件绑定后,您也许可以拆开绑定,但我记不清了,因为它们可能隐藏在StandardProvider
类后面。您在做什么,需要知道特定接口的确切绑定配置?
The only way is to get all instances using
kernel.GetAll<IController>()
. Ninject does not have a set binding mapping. The implementation is determined when the instances are resolved through injection or get calls. This is because Ninject allows for conditional and implicit bindings.If you set up an MVC3 application, you do not actually register your controllers as Ninject will determine which controller to load and what to inject into it. It does this by lazily creating an implicit binding.
The closest you can come is to extend
StandardKernel
and access the protected binding set and find all bindings that are not conditional. Once you have all non-conditional bindings, you may be able to pull apart the binding, but I can't remember exactly as they may be hidden behind theStandardProvider
class.What are you doing that you need to know the exact binding configuration for a particular interface?