【问题标题】:Is a generic type MyType<,> actually a type? [duplicate]泛型类型 MyType<,> 实际上是一种类型吗? [复制]
【发布时间】: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; }

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

【问题讨论】:

    标签: c# .net clr mef


    【解决方案1】:

    What exactly is an “open generic type”。您所指的称为 未绑定的泛型类型,并在同一 post 中进行了解释。 未绑定的泛型类型实际上是一种类型,但是它只能在typeof() 表达式中使用。注意:与 C# 不同,Java 允许像 List&lt;?&gt; 这样的表达式。

    【讨论】:

      【解决方案2】:

      是的,MyType 是一种类型。 这是一个“开放的通用”类型,见What exactly is an "open generic type" in .NET?

      【讨论】:

        猜你喜欢
        • 2021-08-13
        • 2016-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-07
        • 2018-08-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多