【发布时间】:2016-11-28 16:50:24
【问题描述】:
我正在尝试使用 MEF 实现插件框架。我有 3 个项目:
- 宿主项目 (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; }
}
}
开
var asmCat = new AssemblyCatalog(file);
我可以看到,有一个“ReflectionTypeLoadException”和零件清单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";
}
}
}
}
【问题讨论】: