2016-11-28 62 views
0

我試圖實現MEF的插件框架。我有3個項目:DLL負載上的ReflectionTypeLoadException

  • 主機項目(WPF)
  • 接口定義項目(便攜式類庫)
  • 的插件項目(便攜式類庫)

現在在主機,我嘗試加載插件彙編DLL(僅顯示應加載DLL的類):

public class SafeDirectoryCatalog : ComposablePartCatalog 
{ 
    private readonly AggregateCatalog _catalog; 

    public SafeDirectoryCatalog(string directory) 
    { 
     var files = Directory.GetFiles(directory, "*.dll", SearchOption.AllDirectories); 

     _catalog = new AggregateCatalog(); 

     foreach (var file in files) 
     { 
      try 
      { 
       var asmCat = new AssemblyCatalog(file); 

       if (asmCat.Parts.ToList().Count > 0) 
       { 
        _catalog.Catalogs.Add(asmCat); 
       } 

      } 
      catch (ReflectionTypeLoadException) 
      { 
      } 
      catch (BadImageFormatException) 
      { 
      } 
     } 
    } 
    public override IQueryable<ComposablePartDefinition> Parts 
    { 
     get { return _catalog.Parts; } 
    } 
} 

on

var asmCat = new AssemblyCatalog(file); 

我可以看到,有一個「ReflectionTypeLoadException」和零件清單上課emtpy:

Exception Screenshot (VS is German)

這是我的接口定義(輸出是在主機中引用的DLL和插件項目):

namespace HCInterfaces 
{ 
    public interface HomeControlInterface 
    { 
     string GetModuleName(); 
    } 
} 

最後這個是輸出plugin.dll我的插件類:

using HCInterfaces; 
using System.Composition; 

namespace Plugin2 
{ 
    public partial class MainWindow 
    { 
     public MainWindow() 
     { 

     } 


     [Export(typeof(HomeControlInterface))] 
     class BMW : HomeControlInterface 
     { 
      public string GetModuleName() 
      { 
       return "hännschenklein"; 
      } 
     } 

    } 
} 

回答