我不确定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);但是如果我使用强类型,我的exports变量是空的。
var exports = container.GetExports<IQuestionFactory, IQuestionFactoryMetadataView>(typeof(PEMDAS.Core.TimesTables.WorksheetTemplate).Namespace);发布于 2012-10-12 10:21:46
我看不到QuestionFactoryMetadataView,你能展示一下那种类型吗?
有吗?
var exports = container.GetExports<IQuestionFactory, IQuestionFactoryMetadata>(...)工作?
https://stackoverflow.com/questions/12847947
复制相似问题