3

我对此感到有些困惑,这是由于在 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; }

我对这里的类型系统规则感到困惑

4

2 回答 2

5

See What exactly is an “open generic type”</a>. What you are referring to is called an unbound generic type and is explained in the same post. An unbound generic type is actually a type, however it can only be used within a typeof() expression. Note: Unlike C# Java allows expressions like List<?>.

于 2012-11-17T18:52:03.510 回答
0

是的,MyType<,> 是一种类型。这是一个“开放式泛型”类型,请参阅.NET 中的“开放式泛型”到底是什么?

于 2012-11-17T18:50:25.603 回答