2009-05-18 80 views
3

我目前MEF的工作和面临的一些问题如何加载使用MEF(托管扩展框架)从目录中的DLL

我想是从目录中加载的DLL。

第i个扫描目录和

名称属性从各自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; } 

目前我的程序运行正常import语句。我做的是当我检查复选框列表中的插件。它将它移动到“加载”目录中,并且它们的QueryPlugin()方法将查找“加载”目录以搜索插件。

取消选中复选框列表中的插件。我将其移出“装”目录...

我要的是使用batch.RemovePart()方法来获得从一个目录到其它的DLL摆脱这种快速移动的....

注:我不使用手动

batch.AddPart(new DemoFilter1()); 

代替该我用DirectoryCatalog(添加插件成批处理);;

+0

尚无回答:( – Mohsan 2009-05-18 06:29:34

回答

3

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

请注意,如果给定程序集中有多个插件,则此方法可能存在问题。更稳健的方法可以使用filtered catalog过滤单个零件定义。

相关问题