MEF GetExports <T,TMetaDataView>使用AllowMultiple = True返回任何内容

soh*_*hum 6 c# attributes mef metadata

我不太了解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; }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

private void SomeFunction()
{
    // CompositionContainer is an instance of CompositionContainer
    var myExports = CompositionContainer.GetExports<IMyInterface, IMyInterfaceInfo>();
}
Run Code Online (Sandbox Code Playgroud)

在我的情况下myExports总是空的.在我的CompositionContainer中,我的目录中有一个包含两个的Part,两个ExportDefinitions都包含以下内容ContractName:"MyNamespace.IMyInterface".该Metadata还正确地加载按我的出口.

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

我究竟做错了什么?

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

var myExports = CompositionContainer.GetExports<IMyInterface, IDictionary<string, object>>();
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

Fra*_*lli 10

众所周知,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; }
}
Run Code Online (Sandbox Code Playgroud)

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

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

然后,您必须为要导入的元数据定义视图.这必须有一个构造函数,它将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]);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

var myExports = CompositionContainer.GetExports<IMyInterface, MyInterfaceInfoView>();
Run Code Online (Sandbox Code Playgroud)