从 ninject 获取所有具体类型

发布于 2024-12-11 23:26:44 字数 267 浏览 0 评论 0原文

有没有办法使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪恋 2024-12-18 23:26:44

唯一的方法是使用 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 the StandardProvider class.

What are you doing that you need to know the exact binding configuration for a particular interface?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文