我不太理解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<IMyInterface, IMyInterfaceInfo>();
}在我的例子中,myExports总是空的。在我的CompositionContainer中,我的目录中有一个部分有两个ExportDefinitions,它们都带有以下ContractName:"MyNamespace.IMyInterface“。Metadata也会根据我的导出正确加载。
如果我移除AllowMultiple设置器,并且只包含一个导出属性,那么myExports变量现在有一个导出及其加载的元数据。
我做错了什么?
编辑:如果我使用弱类型元数据,我的导出就会突然满足:
var myExports = CompositionContainer.GetExports<IMyInterface, IDictionary<string, object>>();知道为什么吗?
发布于 2012-06-13 07:48:37
众所周知,MEF在处理AllowMultiple = true时存在一些问题。作为一个完整的解释,例如,您可以查看here,无论如何,它来自这样一个事实:元数据保存在字典中,当AllowMultiple为真时,值是数组,并且这样的东西不能映射到IMyInterfaceInfo上。
这是我使用的解决办法。首先,属性应该来自属性,而不是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<string, object> 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<IMyInterface, MyInterfaceInfoView>();https://stackoverflow.com/questions/10988447
复制相似问题