2012-11-03 33 views
1

我有出口IScreen兩班,但是當我輸入相同的使用ImportMany MEF負荷合同的兩倍

[ImportMany(typeof(IScreen))] 
private IEnumerable<Lazy<IScreen,IJIMSMetadata>> _modules; 
public IEnumerable<Lazy<IScreen, IJIMSMetadata>> Modules 
{ 
     get { return _modules; } 
} 

模塊包含IScreen的四個實例,但我只有兩個出口。

這是集裝箱

container = new CompositionContainer(
       new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)))     
       ); 


protected override IEnumerable<Assembly> SelectAssemblies() 
{ 
    string _modulePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Modules"); 
    var files = Directory.GetFiles(_modulePath, "*.dll", SearchOption.AllDirectories); 

    List<Assembly> assemblies = new List<Assembly>(); 
    assemblies.Add(Assembly.GetExecutingAssembly()); 
    foreach (var file in files) 
    { 
     assemblies.Add(Assembly.LoadFrom(file)); 
    } 
    return assemblies; 
} 
+0

告訴我們你是如何構建CompositionContainer中 – m0sa

+0

該集裝箱的貨物使用assemby.load從 –

+0

野生的猜測僅僅是一個類,但位於您裝配的地方在那裏通過它的位置遞歸將加載「同一集結號」的兩倍。例如。它通過'Debug'和'Release'文件夾遞歸? –

回答

2

由於使用一個AggregateCatalog,檢查以確保沒有用於容納執行組件中的位置中添加兩者AssemblyCatalog和DirectoryCatalog。

例如,下面的代碼避免了兩次處理相同的程序集。

var catalog = new AggregateCatalog(); 
var locations = new List<string>(); 
foreach (var loc in GetPluginDirectories()) 
    if (!locations.Contains(loc)) 
    { 
     catalog.Catalogs.Add(new DirectoryCatalog(loc)); 
     locations.Add(loc); 
    } 

var asm = Assembly.GetExecutingAssembly(); 
if (!locations.Contains(Path.GetDirectoryName(asm.Location))) 
    catalog.Catalogs.Add(new AssemblyCatalog(asm));