2009-05-18 81 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過濾單個零件定義。

相關問題