MEF GetExports <t,tmetadataview =“”>使用AllowMultiple = True返回任何内容</t,>

时间:2012-06-11 22:38:23

标签: c# attributes metadata mef

我不太了解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的Part,两个都包含以下ContractName:“MyNamespace.IMyInterface”。我的导出也正确加载了Metadata

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

我做错了什么?

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

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

任何想法为什么?

1 个答案:

答案 0 :(得分:9)

众所周知,在处理AllowMultiple = true时,MEF存在一些问题。有关完整的解释,您可以查看here,无论如何,它源于以下事实:元数据保存在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<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>();