C# 如何创建DirectoryCatalog来搜索插件的子目录

C# 如何创建DirectoryCatalog来搜索插件的子目录,c#,plugins,mef,catalog,C#,Plugins,Mef,Catalog,因此,我的目录目录设置如下所示: DirectoryCatalog directoryCatalog = new DirectoryCatalog(@".\Plugins"); var catalog = new AggregateCatalog(directoryCatalog); var container = new CompositionContainer(catalog); container.ComposeParts(this); 它将在我的Plugins文件夹中找到满足我的导入条

因此,我的目录目录设置如下所示:

DirectoryCatalog directoryCatalog = new DirectoryCatalog(@".\Plugins");
var catalog = new AggregateCatalog(directoryCatalog);
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
它将在我的
Plugins
文件夹中找到满足我的导入条件的任何.dll。现在为了防止我的插件中出现引用冲突,我想把每个插件放在一个单独的文件夹中

e、 g

Plugins->Plugin1->Plugin1.dll

      -> Plugin2 -> plugin2.dll

但是我的
DirectoryCatalog
找不到这些插件。是否可以实现此功能,或者我的插件库必须位于指定的
\Plugins

文件夹中?您可以通过将多个
directorycalogs
添加到
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);
还有一点:

我发现我需要包装
\u aggregateCatalog.Catalogs.Add(catalog)Initialize(…)
下,使用try/catch for
ReflectionTypeLoadException
停止在发现非插件DLL时抛出它