MEF通用类与构造函数作为类型契约

时间:2010-06-04 00:57:21

标签: c# generics mef

我有一个泛型类,它使用TBase作为类型参数。使用MEF,我想要一个它应该导入的通用类型列表。我试着用这个:

1)

[ImportMany(typeof(TBase))]
public List<TBase> ObjectList { get; set; }

2)  
Type IValueType = typeof(TBase)

[ImportMany(IValueType)]
public List<TBase> ObjectList{ get; set; }

3)
[ImportMany(TBase)]
public List<TBase> ObjectList{ get; set; }

第一场演出
{'TBase': an attribute argument cannot use type parameters}

第二个节目
{An object reference is required for the non-static field, method, or property}

第三场演出
{'TBase' is a 'type parameter' but is used like a 'variable'}

我在这做错了什么?我该如何解决?

2 个答案:

答案 0 :(得分:2)

试试following syntax

[ImportMany]
public IEnumerable<TBase> ObjectList{ get; set; }

编辑第一种语法应该有效,因为[ImportMany(typeof(TBase))]是一个法律声明,而ImportMany确实会对其构造函数进行输入/

答案 1 :(得分:0)

MEF需要在编译时可以转换为常量字符串的参数。 由于您使用的是通用的TBase,并且只能在运行时实现,因此无法生成MEF元数据。 尝试使用非泛型接口而不是通用类型TBASE。

相关问题