【发布时间】:2012-11-17 18:45:04
【问题描述】:
由于在 MEF 中导出泛型类型搞乱了,我对此感到有些困惑
我注意到了:
new Dictionary<string,bool>().GetType() == typeof(Dictionary<,>)
false
new Dictionary<string,bool>().GetType().GetGenericTypeDefinition() == typeof(Dictionary<,>)
true
但 Dictionary 本身并不被视为“类型”,因为这实际上会产生编译错误:
new Dictionary<string,bool> as Dictionary<,>
Type expected
new Dictionary<string,bool> is Dictionary<,>
Type expected
所以我的问题是, Dictionary 实际上是一种类型吗? .NET 对待泛型类型是否不同于非泛型类型?
现在在 MEF 中,我可以将通用类导出为
[Export(typeof(MyGenericClass<,>))]
这将满足类似的导入要求
[Import]
public MyGenericClass<string, long> Instance { get; set; }
我对这里的类型系统规则感到困惑
【问题讨论】: