C# 如何使用MEF(托管扩展性框架)从目录加载DLL
我目前与MEF合作,面临一些问题 我想要的是从目录加载DLL 首先,我扫描目录并在字典中保存两件东西 来自相应DLL的Name属性(作为字符串) 和模块名称(作为字符串) 下面是ScandDirectory()代码C# 如何使用MEF(托管扩展性框架)从目录加载DLL,c#,.net,assemblies,mef,C#,.net,Assemblies,Mef,我目前与MEF合作,面临一些问题 我想要的是从目录加载DLL 首先,我扫描目录并在字典中保存两件东西 来自相应DLL的Name属性(作为字符串) 和模块名称(作为字符串) 下面是ScandDirectory()代码 我使用了DirectoryCatalog() 使用AggregateCatalog并将目录中每个程序集的AssemblyCatalog添加到聚合目录中,而不是使用DirectoryCatalog。然后,当选中或取消选中插件时,可以将相应的AssemblyCatalog添加或删除到Ag
我使用了DirectoryCatalog() 使用AggregateCatalog并将目录中每个程序集的AssemblyCatalog添加到聚合目录中,而不是使用DirectoryCatalog。然后,当选中或取消选中插件时,可以将相应的AssemblyCatalog添加或删除到AggregateCatalog 请注意,如果给定程序集中有多个插件,这种方法可能会出现问题。一种更稳健的方法可以是使用
private void ScanPluginDirectory()
{
catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\plugin"));
container = new CompositionContainer(catalog);
batch = new CompositionBatch();
batch.AddPart(this);
container.Compose(batch);
pluginDictionary = new Dictionary<String, String>();
foreach (IFilter filter in filters)
{
Type t = filter.GetType();
pluginDictionary.Add(filter.Name, t.Module.Name);
}
}
[Import]
public IEnumerable<IFilter> filters { get; set; }
batch.AddPart(new DemoFilter1());