为什么运行时将泛型类型显示为"GenericType`n"?

rem*_*mio 5 .net generics reflection types

为什么它不显示真实类型(例如:List <string>而不是List`1)?
这个奇怪的(对我来说)符号来自哪里?

Meh*_*ari 9

List<T>具有一个类型参数的泛型的CLR名称是

System.Collections.Generic.List`1
Run Code Online (Sandbox Code Playgroud)

List<T> 是命名该类型的C#样式.

符号是一种定义的约定,使您能够通过参数计数重载泛型类型.如果声明一个以List两个类型参数命名的类(例如List<T,U>),编译器将能够选择正确的类.它将被命名为:

System.Collections.Generic.List`2
Run Code Online (Sandbox Code Playgroud)

并且List将命名一个非泛型类:

System.Collections.Generic.List
Run Code Online (Sandbox Code Playgroud)

需要注意的是,CLR不要求泛型类型被命名为这样.这只是编译器能够选择正确类型的惯例.

  • `List <T>`成为List \`1.这是"开放泛型类型定义".`List <string>`的闭合定义将成为List \`1 [System.String]. (2认同)

the*_*oop 3

`1 是泛型类型的 MSIL 语法 -List<T>特定于 C#。更多 MSIL 示例:

List<T> == System.Collections.Generic.List`1
Run Code Online (Sandbox Code Playgroud)
List<string> == System.Collections.Generic.List`1[System.String]
Run Code Online (Sandbox Code Playgroud)
Dictionary<string, int> == 
   System.Collections.Generic.Dictionary`2[System.String, System.Int32]
Run Code Online (Sandbox Code Playgroud)