如何使用MEF Inherited Export&元数据? [英] How to use MEF Inherited Export & MetaData?
问题描述
我有一个界面:
[InheritedExport(typeof(IMetric))]
public interface IMetric { ... }
我有一个Meta属性接口:
I have a Meta attribute interface:
public interface IMetricAttribute { ... }
和实现它的属性:
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class MetricAttribute : ExportAttribute, IMetricAttribute {
public string MetricName { get; set; }
public string MetricDescription { get; set; }
public MetricAttribute(string name, string description)
: base(typeof(MetricAttribute)) {
this.MetricName = name;
this.MetricDescription = description;
}
}
然后我有两个类:
[Metric("MetricA","MetricA")]
public class MetricA: IMetric { ... }
[Export(typeof(IMetric))] <<<< THIS IS IMPORTANT
[Metric("MetricB", "MetricB")]
public class MetricB: IMetric { ... }
然后我尝试导入指标(我可以在cataloge中看到)
I then try to import the metrics ( i can see both in the cataloge)
以下返回MetricA AND MetricB
The following returns be MetricA AND MetricB
var metrics = compositionContainer.GetExports<IMetric>();
但是,以下只返回MetricB和NOT MetricA
However the following returns ONLY MetricB and NOT MetricA
var metrics = compositionContainer.GetExports<IMetric, IMetricAttribute>();
任何想法为什么?
(注意在MetricB上重复导出(已从实施IMetric中导出)
(note the duplicate export on MetricB (it already has it from implementing IMetric))
感谢
David
推荐答案
我第一次看到这种行为,但从我能理解的,导出类型级别。因此,给定:
First time I've seen this behaviour, but from what I can understand, metadata is generated per-export at the type level. So, given:
[Metric("MetricA", "MetricA")]
public class MetricA : IMetric
{
}
您有两种此类型的导出。您拥有 MetricAttribute
所隐含提供的 MetricA
的导出,并且您继承了<$ c
You have two exports for this type. You have the export of MetricA
which is implictly provided by your MetricAttribute
, and you have the inherited export for IMetric
provided by the InheritedExport(typeof(IMetric))
attribute on your interface.
如果你看容器,你会注意到 MetricA
定义了两个导出。这是第一个,其元数据:
If you look at the container, you'll notice two exports defined for MetricA
. Here is the first, with its metadata:
这是第二个:
您会注意到元数据是在导出 MetricA
,而不是继承的导出。如果我添加了另一个导出,让 [Export(test)]
到 MetricA
定义,对于名为test的合同, MetricName
和 MetricDescription
具有相同的元数据项。这表明在分析类型时,将识别导出属性,并且创建的导出定义包括抽象树中同一级别上指定的元数据。
You'll notice that the metadata is done on the export of MetricA
, not the inherited export. If I added a further export, lets say [Export("test")]
to MetricA
, you get another export definition, with the same metadata items for MetricName
and MetricDescription
for the contract named "test". This shows you that as the type is analysed, the export attribute is identified, and the export definition that is created includes the metadata specified at the same level in the abstraction tree.
InheritedExport
,并修改您的 MetricAttribute
的定义,最简单的方法来做你想要的, to:
The easiest way to do what you want, is to drop out the InheritedExport
, and modify your definition of your MetricAttribute
to:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false), MetadataAttribute]
public class MetricAttribute : ExportAttribute, IMetricAttribute
{
public MetricAttribute(string name, string description)
: base(typeof(IMetric))
{
this.MetricName = name;
this.MetricDescription = description;
}
public string MetricName { get; private set; }
public string MetricDescription { get; private set; }
}
typeof(IMetric)到基本的 ExportAttribute
构造函数。然后,您可以正确获取 GetExports< IMetric>()
和 GetExports< IMetric,IMetricAttribute>()
Where you then pass in typeof(IMetric)
to the base ExportAttribute
constructor. You then correctly get the two exports for GetExports<IMetric>()
and GetExports<IMetric, IMetricAttribute>()
.
这篇关于如何使用MEF Inherited Export&元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!