c# - 泛型类型 MyType<,> 实际上是一个类型吗?

标签 c# .net clr mef

<分区>

由于在 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; }

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

最佳答案

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

关于c# - 泛型类型 MyType<,> 实际上是一个类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13433619/

相关文章:

c# - clr 线程模型信息?

c# - 手动使用真正的 resx 解析 IStringLocalizer

c# - 预先计算循环(或其他任何地方)中使用的常量是否有益?

c# - 创建松散耦合/可扩展的软件架构

c# - HttpWebResponse 内容编码 : identity

c# - ImmutableArray<T> 和 ImmutableList<T> 的区别

c# - 在 C# 中,控件不能从一个案例标签 ('default:' ) 掉落到另一个案例标签

c# - System.IO.File.Create 锁定一个文件

c# - 在 C# 中使用 foreach 循环时的内存分配

CLR/快速调用 : How are large value types passed internally to called functions?