简体   繁体   English

使用Ninject进行类型激活

[英]Using Ninject for Type Activation

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 )? 是否有内置于Ninject的任何方法允许在给定其程序集限定的名称的情况下动态实例化任何给定类型(无需在自定义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). 我当然可以使用Activator.CreateInstance但由于我已经在使用Ninject,我宁愿坚持使用它而不是使用这种方法(顺便说一下,我听说这可能很慢)。

You could use below, which will look for all classes which extend NinjectModule in a given assembly and load them: 您可以在下面使用,它将查找在给定程序集中扩展NinjectModule并加载它们的所有类:

var kernel = new StandardKernel();
kernel.Load(Assembly.Load("assembly.dll"));

Ninject needs to have bindings to create instances. Ninject需要绑定才能创建实例。 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. 看看Ninject.Extensions.Conventions。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM