我试图用MEF实现一个插件框架。我有3个项目:
现在在主机中,我尝试加载插件组件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; }
}
}
在
var asmCat = new AssemblyCatalog(file);
我可以看到,有一个&#34; ReflectionTypeLoadException&#34;和零件清单es 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";
}
}
}
}