【问题标题】:How to create a DirectoryCatalog that will search sub directories for plugins如何创建一个目录目录来搜索插件的子目录
【发布时间】:2014-11-13 09:41:04
【问题描述】:

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

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文件夹中

【问题讨论】:

    标签: c# plugins mef catalog


    【解决方案1】:

    您可以通过将多个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);
    

    【讨论】:

      【解决方案2】:

      还有这个: https://github.com/MefContrib/MefContrib/blob/master/src/MefContrib/Hosting/RecursiveDirectoryCatalog.cs

      我发现我需要将_aggregateCatalog.Catalogs.Add(catalog); 包裹在Initialize(...) 下,并使用ReflectionTypeLoadException 的try/catch 来阻止它在找到非插件dll 时抛出。

      【讨论】:

        猜你喜欢
        • 2016-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-13
        • 2010-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多