0

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

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
{
    ...
}

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

        var exports = container.GetExports<IQuestionFactory, IDictionary<string, object>>(typeof(PEMDAS.Core.TimesTables.WorksheetTemplate).Namespace);

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

        var exports = container.GetExports<IQuestionFactory, IQuestionFactoryMetadataView>(typeof(PEMDAS.Core.TimesTables.WorksheetTemplate).Namespace);
4

1 回答 1

0

我没有看到 QuestionFactoryMetadataView 你能显示那种类型吗?

var exports = container.GetExports<IQuestionFactory, IQuestionFactoryMetadata>(...)

工作?

于 2012-10-12T02:21:46.103 回答