Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用MEF(托管扩展性框架)从目录加载DLL_C#_.net_Assemblies_Mef - Fatal编程技术网

C# 如何使用MEF(托管扩展性框架)从目录加载DLL

C# 如何使用MEF(托管扩展性框架)从目录加载DLL,c#,.net,assemblies,mef,C#,.net,Assemblies,Mef,我目前与MEF合作,面临一些问题 我想要的是从目录加载DLL 首先,我扫描目录并在字典中保存两件东西 来自相应DLL的Name属性(作为字符串) 和模块名称(作为字符串) 下面是ScandDirectory()代码 我使用了DirectoryCatalog() 使用AggregateCatalog并将目录中每个程序集的AssemblyCatalog添加到聚合目录中,而不是使用DirectoryCatalog。然后,当选中或取消选中插件时,可以将相应的AssemblyCatalog添加或删除到Ag

我目前与MEF合作,面临一些问题

我想要的是从目录加载DLL

首先,我扫描目录并在字典中保存两件东西

来自相应DLL的Name属性(作为字符串)

和模块名称(作为字符串)

下面是ScandDirectory()代码


我使用了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());