MEF GetExports 使用AllowMultiple = True返回任何内容

我不太了解MEF,所以希望这是我认为它的工作原理的简单解决方法。

我正在尝试使用MEF来获取有关类的一些信息以及如何使用它。 我正在使用元数据选项来尝试实现此目的。 我的接口和属性如下所示:

public interface IMyInterface { } public interface IMyInterfaceInfo { Type SomeProperty1 { get; } double SomeProperty2 { get; } string SomeProperty3 { get; } } [MetadataAttribute] [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class ExportMyInterfaceAttribute : ExportAttribute, IMyInterfaceInfo { public ExportMyInterfaceAttribute(Type someProperty1, double someProperty2, string someProperty3) : base(typeof(IMyInterface)) { SomeProperty1 = someProperty1; SomeProperty2 = someProperty2; SomeProperty3 = someProperty3; } public Type SomeProperty1 { get; set; } public double SomeProperty2 { get; set; } public string SomeProperty3 { get; set; } } 

使用该属性修饰的类如下所示:

 [ExportMyInterface(typeof(string), 0.1, "whoo data!")] [ExportMyInterface(typeof(int), 0.4, "asdfasdf!!")] public class DecoratedClass : IMyInterface { } 

尝试使用导入的方法如下所示:

 private void SomeFunction() { // CompositionContainer is an instance of CompositionContainer var myExports = CompositionContainer.GetExports(); } 

在我的情况下, myExports总是空的。 在我的CompositionContainer中,我的目录中有一个Part,它有两个ExportDefinitions ,都有以下ContractName :“MyNamespace.IMyInterface”。 我的导出也正确加载了Metadata

如果我删除AllowMultiple setter并且只包含一个导出属性,则myExports变量现在具有单个导出及其加载的元数据。

我究竟做错了什么?

编辑:如果我使用弱类型的元数据,我的导出突然满足:

 var myExports = CompositionContainer.GetExports<IMyInterface, IDictionary>(); 

有什么想法吗?

众所周知,MEF在处理AllowMultiple = true时存在一些问题。 有关完整的解释,您可以查看此处 ,无论如何,它源于以下事实:元数据保存在Dictionary中,当AllowMultiple为true时,值为数组,并且无法在IMyInterfaceInfo上映射此类内容。

这是我使用的解决方法。 首先,您的属性应该从Attribute派生,而不是从ExportAttribute派生:

 [MetadataAttribute] [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class ExportMyInterfaceAttribute : Attribute, IMyInterfaceInfo { public ExportMyInterfaceAttribute(Type someProperty1, double someProperty2, string someProperty3) { SomeProperty1 = someProperty1; SomeProperty2 = someProperty2; SomeProperty3 = someProperty3; } public Type SomeProperty1 { get; set; } public double SomeProperty2 { get; set; } public string SomeProperty3 { get; set; } } 

这意味着导出的类应具有3个属性,标准导出和自定义属性:

 [Export(typeof(IMyInterface))] [ExportMyInterface(typeof(string), 0.1, "whoo data!")] [ExportMyInterface(typeof(int), 0.4, "asdfasdf!!")] public class DecoratedClass : IMyInterface 

然后,您必须为要导入的元数据定义视图。 这必须有一个构造函数,它将IDictionary作为参数。 像这样的东西:

 public class MyInterfaceInfoView { public IMyInterfaceInfo[] Infos { get; set; } public MyInterfaceInfoView(IDictionary aDict) { Type[] p1 = aDict["SomeProperty1"] as Type[]; double[] p2 = aDict["SomeProperty2"] as double[]; string[] p3 = aDict["SomeProperty3"] as string[]; Infos = new ExportMyInterfaceAttribute[p1.Length]; for (int i = 0; i < Infos.Length; i++) Infos[i] = new ExportMyInterfaceAttribute(p1[i], p2[i], p3[i]); } } 

现在你应该能够成功打电话了

 var myExports = CompositionContainer.GetExports();