首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MEF GetExports<T,TMetaDataView>在AllowMultiple = True中不返回任何内容

MEF GetExports<T,TMetaDataView>在AllowMultiple = True中不返回任何内容
EN

Stack Overflow用户
提问于 2012-06-11 22:38:22
回答 1查看 5.7K关注 0票数 6

我不太理解MEF,所以希望这是一个简单的修正,我认为它是如何工作的。

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

代码语言:javascript
复制
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; }
}

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

代码语言:javascript
复制
[ExportMyInterface(typeof(string), 0.1, "whoo data!")]
[ExportMyInterface(typeof(int), 0.4, "asdfasdf!!")]
public class DecoratedClass : IMyInterface
{
}

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

代码语言:javascript
复制
private void SomeFunction()
{
    // CompositionContainer is an instance of CompositionContainer
    var myExports = CompositionContainer.GetExports<IMyInterface, IMyInterfaceInfo>();
}

在我的例子中,myExports总是空的。在我的CompositionContainer中,我的目录中有一个部分有两个ExportDefinitions,它们都带有以下ContractName:"MyNamespace.IMyInterface“。Metadata也会根据我的导出正确加载。

如果我移除AllowMultiple设置器,并且只包含一个导出属性,那么myExports变量现在有一个导出及其加载的元数据。

我做错了什么?

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

代码语言:javascript
复制
var myExports = CompositionContainer.GetExports<IMyInterface, IDictionary<string, object>>();

知道为什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-13 07:48:37

众所周知,MEF在处理AllowMultiple = true时存在一些问题。作为一个完整的解释,例如,您可以查看here,无论如何,它来自这样一个事实:元数据保存在字典中,当AllowMultiple为真时,值是数组,并且这样的东西不能映射到IMyInterfaceInfo上。

这是我使用的解决办法。首先,属性应该来自属性,而不是ExportAttribute:

代码语言:javascript
复制
[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个属性,一个标准导出和您的自定义属性:

代码语言:javascript
复制
[Export(typeof(IMyInterface))]
[ExportMyInterface(typeof(string), 0.1, "whoo data!")]
[ExportMyInterface(typeof(int), 0.4, "asdfasdf!!")]
public class DecoratedClass : IMyInterface

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

代码语言:javascript
复制
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]);
    }
}

现在您应该能够成功地调用

代码语言:javascript
复制
var myExports = CompositionContainer.GetExports<IMyInterface, MyInterfaceInfoView>();
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10988447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档