C# MEF2轻量级系统。包含元数据的组合

C# MEF2轻量级系统。包含元数据的组合,c#,composition,.net-standard-2.0,mef2,C#,Composition,.net Standard 2.0,Mef2,对于如何使用轻量级MEF2 System.Composition,完全缺乏示例,这使得这个问题变得棘手。我只使用了System.Composition(而不是System.ComponentModel.Composition) 我想导入具有元数据的部件。我使用的是属性代码。不幸的是,当我试图得到出口,我得到了一个大的脂肪空 元数据属性: namespace Test.ConfigurationReaders { 使用制度; 使用系统组成; [元数据属性] [AttributeUsage(Attr

对于如何使用轻量级MEF2 System.Composition,完全缺乏示例,这使得这个问题变得棘手。我只使用了
System.Composition
(而不是
System.ComponentModel.Composition

我想导入具有元数据的部件。我使用的是属性代码。不幸的是,当我试图得到出口,我得到了一个大的脂肪空

元数据属性

namespace Test.ConfigurationReaders
{
使用制度;
使用系统组成;
[元数据属性]
[AttributeUsage(AttributeTargets.Class,AllowMultiple=false)]
公共类ExportReaderAttribute:ExportAttribute
{
公共字符串读取器{get;set;}
}
}
导出

namespace Test.ConfigurationReaders
{
使用System.Collections.Generic;
使用系统配置;
[ExportReader(Reader=“CONFIG”)]
公共类ConfigReader:IConfigurationReader
{
公共IEnumerable ReadAll()
{
返回新列表();
}
}
}
ImportMany
并对元数据进行筛选:

namespace Test.Core
{
使用制度;
使用System.Collections.Generic;
使用系统组成;
使用System.Composition.Hosting;
使用系统配置;
使用System.Linq;
运用系统反思;
使用ConfigurationReaders;
公开密封等级考试
{
静态测试()
{
新测试();
}
私人测试()
{
SetImports();
Reader=SetReader();
}
[进口数量]
私有静态IEnumerable读取器{get;set;}
公共静态IConfigurationReader{get;private set;}
私有静态IConfigurationReader SetReader()
{
//根据应用设置设置配置读取器。
变量源=
ConfigurationManager.AppSettings[“源”]
??配置;
var readers=readers.ToList();
变量读取器=
Readers.ToList()
.找到(
f=>
f、 Metadata.Reader.Equals(
来源:,
StringComparison.Ordinalingorecase)
.CreateExport()
价值
返回读取器;
}
私有void SetImports()
{
变量配置=
新建ContainerConfiguration().WithAssembly(
新名单{
typeof(IConfigurationReader.Assembly});
var container=configuration.CreateContainer();
集装箱。满足进口(本);
Readers=container.GetExports();
}
}
}
不幸的是,
读卡器
为空。这是这里的示例代码,但我可以看到在我的实际代码中没有元数据的部分,这样至少可以正常工作

如何填充
读卡器


我正试图将.NET Standard 2.0作为目标,并从.NET Core中使用它。

解决方案是更改导出类的属性:

[导出(类型(IConfigurationReader))]
[导出元数据(“读取器”、“配置”)]
公共类ConfigReader:IConfigurationReader
{
}