如何创建将在子目录中搜索插件的DirectoryCatalog

use*_*er1 3 c# plugins mef catalog

所以我的目录目录设置如下:

DirectoryCatalog directoryCatalog = new DirectoryCatalog(@".\Plugins");
var catalog = new AggregateCatalog(directoryCatalog);
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
Run Code Online (Sandbox Code Playgroud)

哪个会在我的Plugins文件夹中找到符合我的导入条件的.dll .现在为了防止插件中的引用冲突,我想将每个插件放在一个单独的文件夹中.

例如

插件 - > Plugin1 - > plugin1.dll

      -> Plugin2 -> plugin2.dll
Run Code Online (Sandbox Code Playgroud)

但我DirectoryCatalog找不到这些插件.可以实现这个,还是我的插件库必须在指定的.\Plugins文件夹中

Igo*_*gor 12

您可以通过向此属性添加多个DirectoryCatalogs来解决您的问题,AggregateCatalog.Catalogs如下所示:

var catalog = new AggregateCatalog();
// iterate over all directories in .\Plugins dir and add all Plugin* dirs to catalogs
foreach (var path in Directory.EnumerateDirectories(@".\Plugins", "Plugin*", SearchOption.TopDirectoryOnly))
{
    catalog.Catalogs.Add(new DirectoryCatalog(path));
}

_container = new CompositionContainer(catalog);
this._container.ComposeParts(this);
Run Code Online (Sandbox Code Playgroud)