扫描组件以查找匹配的接口并加载为模块

扫描组件以查找匹配的接口并加载为模块

本文介绍了Ninject->扫描组件以查找匹配的接口并加载为模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ninject.Extensions.Conventions的早期版本中,扫描目录中的程序集,按接口过滤类,然后加载所有包含的ninject模块非常容易.

In earlier versions of Ninject.Extensions.Conventions, it was pretty easy to scan a directory for assemblies, filter classes by interface and then load all containing ninject modules.

kernel.Scan(scanner =>
    scanner.FromAssembliesInPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
    scanner.AutoLoadModules();
    scanner.WhereTypeInheritsFrom<IPlugin>());

public class MyPlugin : NinjectModule, IPlugin {

     public override void Load() {
          Bind<IRepositoryFromPluginHost>().To<MyPluginImpl>().Named("special");
     }
}

但是,最近更新到最新版本后,一切似乎消失了,我无法

However, after I updated lately to the newest release, everything seems gone and I'm unable to

  1. 自动加载模块
  2. 按接口过滤类型

有人对此有解决方案吗?

Does anybody have a solution for this?

推荐答案

仍有 https://github.com/ninject/ninject.extensions.conventions 扩展名.但是,界面已更改为类似以下内容:

There's still the https://github.com/ninject/ninject.extensions.conventions extension.However, the interface has changed, to something along the lines of:

kernel.Bind(x =>
{
    x.FromAssembliesInPath("somepath")
     .IncludingNonePublicTypes()
     .SelectAllClasses()
     .InheritedFrom<IPlugin>()
     .BindDefaultInterface() // Binds the default interface to them;
});

更新:如何使用约定扩展名(如上所述)将所有IPlugin绑定到IPlugin,然后执行:

Update:How about you bind all IPlugin to IPlugin using the conventions extension (as above), and then do:

var plugins = IResolutionRoot.GetAll<IPlugin>();
kernel.Load(plugins);

这篇关于Ninject-&gt;扫描组件以查找匹配的接口并加载为模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:18