3

我目前正在与 MEF 合作并面临一些问题

我想要的是从目录中加载 dll。

首先我扫描目录并将两件事保存在字典中

来自相应 DLL 的名称属性(作为字符串)

和模块名称(作为字符串)

这是 ScanDirectory() 代码

private void ScanPluginDirectory()
{
    catalog = new AggregateCatalog();

    catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\plugin"));            
    container = new CompositionContainer(catalog);

    batch = new CompositionBatch();
    batch.AddPart(this);        

    container.Compose(batch);    

    pluginDictionary = new Dictionary<String, String>();
    foreach (IFilter filter in filters)
    {
        Type t = filter.GetType();
        pluginDictionary.Add(filter.Name, t.Module.Name);
    }
}

并在复选框列表中显示他们的名字。从复选框中选择 dll。

我有进口声明

[Import]
public IEnumerable<IFilter> filters { get; set; }

目前我的程序运行良好。我所做的是当我从复选框列表中检查插件时。它将其移动到“已加载”目录中,并且 QueryPlugin() 方法会查看“已加载”目录以搜索插件。

从复选框列表中取消选中插件后。我将它移出“加载”目录...

我想要的是使用 batch.RemovePart() 方法来摆脱 dll 从一个目录到另一个目录的快速移动......

注意:我不会使用手动将插件添加到批处理中

batch.AddPart(new DemoFilter1());

而不是这个我使用 DirectoryCatalog();;

4

1 回答 1

3

不使用 DirectoryCatalog,而是使用 AggregateCatalog 并将目录中每个程序集的 AssemblyCatalog 添加到聚合目录中。然后当一个插件被选中或取消选中时,您可以将相应的 AssemblyCatalog 添加或删除到 AggregateCatalog。

请注意,如果给定程序集中有多个插件,则此方法可能会出现问题。更强大的方法是使用过滤目录过滤单个零件定义。

于 2009-05-18T17:09:19.077 回答