[英]How to create a DirectoryCatalog that will search sub directories for plugins
所以我的目錄目錄設置如下:
DirectoryCatalog directoryCatalog = new DirectoryCatalog(@".\Plugins");
var catalog = new AggregateCatalog(directoryCatalog);
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
哪個會在我的Plugins
文件夾中找到符合我的導入條件的.dll。 現在為了防止插件中的引用沖突,我想將每個插件放在一個單獨的文件夾中。
例如
插件 - > Plugin1 - > plugin1.dll
-> Plugin2 -> plugin2.dll
但我的DirectoryCatalog
沒有找到這些插件。 是否可以實現這一點,或者我的插件庫必須在指定的.\\Plugins
文件夾中
您可以通過向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);
還有這個: https : //github.com/MefContrib/MefContrib/blob/master/src/MefContrib/Hosting/RecursiveDirectoryCatalog.cs
我發現我需要包裝_aggregateCatalog.Catalogs.Add(catalog);
在Initialize(...)
使用一個try / catch for ReflectionTypeLoadException
來阻止它在找到非插件dll時拋出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.