使用 Ninject 进行类型激活

发布于 2024-12-08 13:26:15 字数 191 浏览 4 评论 0原文

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 技术交流群。

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

发布评论

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

评论(2

遇见了你 2024-12-15 13:26:15

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.

澉约 2024-12-15 13:26:15

您可以使用下面的方法,它将查找在给定程序集中扩展 NinjectModule 的所有类并加载它们:

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

You could use below, which will look for all classes which extend NinjectModule in a given assembly and load them:

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