我试图创建一个可以动态加载实现接口“IPlugin”的类的应用程序,我有:
var catalog = new AssemblyCatalog(typeof(Shell).Assembly);
var externalCatalog = new DirectoryCatalog(@".\Modules");
var container = new CompositionContainer(catalog);
var a = new AggregateCatalog(externalCatalog, catalog);
但是当我试图获得出口时:
CompositionContainer __container = new CompositionContainer(a);
//get all the exports and load them into the appropriate list tagged with the importmany
__container.Compose(batch);
var yyyy = __container.GetExports<IModule>();
它在外部程序集“Rejseplan”中找不到我的“IPlugin”。
<小时/> “Rejseplan”插件的实现:(未加载的插件)
namespace Rejseplan
{
[ModuleExport(typeof(IPlugin), InitializationMode = InitializationMode.WhenAvailable)]
class RejseplanModule : IModule, IPlugin
{
private readonly IRegionViewRegistry regionViewRegistry;
[ImportingConstructor]
public RejseplanModule(IRegionViewRegistry registry)
{
this.regionViewRegistry = registry;
}
public void Initialize()
{
regionViewRegistry.RegisterViewWithRegion("MainRegion", typeof(Views.DepartureBoard));
}
string IPlugin.Name
{
get { throw new NotImplementedException(); }
}
string IPlugin.Version
{
get { throw new NotImplementedException(); }
}
string IPlugin.TabHeader
{
get { throw new NotImplementedException(); }
}
}
}
“测试”插件(GETS加载的插件)的实施:
namespace HomeSystem
{
[Export(typeof(IPlugin))]
[ModuleExport(typeof(IModule), InitializationMode = InitializationMode.WhenAvailable)]
public class Test : IModule, IPlugin
{
public void Initialize()
{
}
public string Name
{
get { return "Test"; }
}
public string Version
{
get { return "Tis"; }
}
public string TabHeader
{
get { return "Tabt"; }
}
}
}
希望你们能帮助你们! :)
答案 0 :(得分:0)
尝试这样做。
var directoryPath = "path to dll folder";
var asmCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var directoryCatalog = new DirectoryCatalog(directoryPath, "*.dll");
var aggregateCatalog = new AggregateCatalog();
aggregateCatalog.Catalogs.Add(asmCatalog);
aggregateCatalog.Catalogs.Add(directoryCatalog);
var container = new CompositionContainer(aggregateCatalog);
container.ComposeParts(this);
答案 1 :(得分:0)
__container.GetExports<IModule>();
您必须添加正确的导出属性。
RejseplanModule不是MEF标记的导出类型IModule。如果它是一个错字,你能检查你的代码吗?至少它应该是以下(见typeof( IModule ))
编辑:
外部dll
[Export(typeof(IModule))]//<-- remove this if its handled by your custom ModulExport Attribute
[ModuleExport(typeof(IModule), InitializationMode = InitializationMode.WhenAvailable)]
class RejseplanModule : IModule, IPlugin
{...}
您的主应用程序(来自上面的fhnaseer的代码)
var directoryPath = "path to dll folder";
var asmCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var directoryCatalog = new DirectoryCatalog(directoryPath, "*.dll");
var aggregateCatalog = new AggregateCatalog();
aggregateCatalog.Catalogs.Add(asmCatalog);
aggregateCatalog.Catalogs.Add(directoryCatalog);
var container = new CompositionContainer(aggregateCatalog);
var allIModulPlugins = container.GetExports<IModule>();