首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GetExports<T,MEF中的TMetaDataView>不返回AllowMultiple = True的元素

GetExports<T,MEF中的TMetaDataView>不返回AllowMultiple = True的元素
EN

Stack Overflow用户
提问于 2012-10-12 04:25:36
回答 1查看 312关注 0票数 0

我不确定MEF是如何工作的。我用AllowMultiple=true创建了一个自定义属性。这就是它:

代码语言:javascript
复制
public interface IQuestionFactoryMetadataView
{
    IQuestionFactoryMetadata[] Metadatas { get; }
}

public interface IQuestionFactoryMetadata
{
    Levels Difficulty { get; }
    int Quantity { get; }
}

[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class ExportQuestionFactoryAttribute : ExportAttribute, IQuestionFactoryMetadata
{
    public ExportQuestionFactoryAttribute(Type type, Levels difficulty, int quantity)
        : base(type.Namespace, typeof(IQuestionFactory))
    {
        this.Difficulty = difficulty;
        this.Quantity = quantity;
    }

    public Levels Difficulty { get; private set; }
    public int Quantity { get; private set; }
}

// 
[ExportQuestionFactory(typeof(NonConcreteQuestionFactory), Levels.Beginner, 10)]
[ExportQuestionFactory(typeof(NonConcreteQuestionFactory), Levels.Medium, 20)]
[ExportQuestionFactory(typeof(NonConcreteQuestionFactory), Levels.Expert, 30)]
[ExportQuestionFactory(typeof(NonConcreteQuestionFactory), Levels.Master, 40)]
public class NonConcreteQuestionFactory : QuestionTemplateFactory
{
    ...
}

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

代码语言:javascript
复制
        var exports = container.GetExports<IQuestionFactory, IDictionary<string, object>>(typeof(PEMDAS.Core.TimesTables.WorksheetTemplate).Namespace);

但是如果我使用强类型,我的exports变量是空的。

代码语言:javascript
复制
        var exports = container.GetExports<IQuestionFactory, IQuestionFactoryMetadataView>(typeof(PEMDAS.Core.TimesTables.WorksheetTemplate).Namespace);
EN

回答 1

Stack Overflow用户

发布于 2012-10-12 10:21:46

我看不到QuestionFactoryMetadataView,你能展示一下那种类型吗?

有吗?

代码语言:javascript
复制
var exports = container.GetExports<IQuestionFactory, IQuestionFactoryMetadata>(...)

工作?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12847947

复制
相关文章

相似问题

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