[英]How to create a DirectoryCatalog that will search sub directories for plugins
So I have my directory catalog set up shown below: 所以我的目录目录设置如下:
DirectoryCatalog directoryCatalog = new DirectoryCatalog(@".\Plugins");
var catalog = new AggregateCatalog(directoryCatalog);
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
Which will find any .dll's in my Plugins
folder that meet my import criteria. 哪个会在我的Plugins
文件夹中找到符合我的导入条件的.dll。 Now To prevent reference clashes in my plugins I would like to put each plugin in a seperate folder. 现在为了防止插件中的引用冲突,我想将每个插件放在一个单独的文件夹中。
eg 例如
Plugins -> Plugin1 -> plugin1.dll 插件 - > Plugin1 - > plugin1.dll
-> Plugin2 -> plugin2.dll
But my DirectoryCatalog
doesn't find these plugins. 但我的DirectoryCatalog
没有找到这些插件。 Is it possible to implement this or do my plugin libraries have to be in that specified .\\Plugins
folder 是否可以实现这一点,或者我的插件库必须在指定的.\\Plugins
文件夹中
You can solve your problem by adding multiple DirectoryCatalogs
to AggregateCatalog.Catalogs
property like this: 您可以通过向AggregateCatalog.Catalogs
属性添加多个DirectoryCatalogs
来解决您的问题,如下所示:
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);
There is also this: https://github.com/MefContrib/MefContrib/blob/master/src/MefContrib/Hosting/RecursiveDirectoryCatalog.cs 还有这个: https : //github.com/MefContrib/MefContrib/blob/master/src/MefContrib/Hosting/RecursiveDirectoryCatalog.cs
I found I needed to wrap the _aggregateCatalog.Catalogs.Add(catalog);
我发现我需要包装_aggregateCatalog.Catalogs.Add(catalog);
under Initialize(...)
with a try/catch for ReflectionTypeLoadException
to stop it throwing when it found non-plugin dlls. 在Initialize(...)
使用一个try / catch for ReflectionTypeLoadException
来阻止它在找到非插件dll时抛出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.