使用 Ninject 进行类型激活
Ninject 是否有任何内置方法允许动态实例化任何给定类型(给定其程序集限定名称)(无需在自定义 NinjectModule
中手动绑定类型)?
我当然可以使用 Activator.CreateInstance ,但由于我已经在使用 Ninject,所以我更愿意坚持使用它而不是使用此方法(顺便说一句,我听说这可能会很慢)。
Is there any method built-in to Ninject which allows for dynamic instantiation of any given type given its assembly-qualified name (without the need to bind the types manually in a custom NinjectModule
)?
I could of course use Activator.CreateInstance
but since I am already using Ninject I would prefer sticking to it rather than using this method (which I heard may be pretty slow, by the way).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Ninject 需要有绑定来创建实例。这些绑定可以静态创建或使用约定创建。根据您想要做什么,约定可能就是您所需要的。看一下 Ninject.Extensions.Conventions。
Ninject needs to have bindings to create instances. These bindings can either be created statically or using conventions. Depending on what you want to do, conventions might be what you need. Have a look at Ninject.Extensions.Conventions.
您可以使用下面的方法,它将查找在给定程序集中扩展 NinjectModule 的所有类并加载它们:
You could use below, which will look for all classes which extend NinjectModule in a given assembly and load them: