C# 为什么在CIL的函数定义中泛型类型看起来像(!!T)

C# 为什么在CIL的函数定义中泛型类型看起来像(!!T),c#,generics,cil,C#,Generics,Cil,为什么泛型类型在函数定义处看起来像(!!T),在CIL中在调用者处看起来像(!!0)。在某些场景中,泛型类型的参数类似于(!0)。背后的秘密是什么?或者为什么它是这样设计的 先谢谢你 下面是C#中的代码: private static void Main(字符串[]args) { Test(); 测试列表(新列表{1,2,3},1,2,3,4,5,6,7,8); Console.ReadKey(); } 专用静态无效测试列表(列表列表,T项,T t2,T t3,T t4,T t5,T t6,T

为什么泛型类型在函数定义处看起来像(!!T),在CIL中在调用者处看起来像(!!0)。在某些场景中,泛型类型的参数类似于(!0)。背后的秘密是什么?或者为什么它是这样设计的

先谢谢你

下面是C#中的代码:

private static void Main(字符串[]args)
{
Test();
测试列表(新列表{1,2,3},1,2,3,4,5,6,7,8);
Console.ReadKey();
}
专用静态无效测试列表(列表列表,T项,T t2,T t3,T t4,T t5,T t6,T t7,int t8)
{
Console.WriteLine(item==null);
WriteLine(“集合包含{0}?{1}”,项,列表。包含(项));
}
调用TestList的CIL代码:

IL_002e:  call       void BoxingAndUnboxing.Program::TestList<int32>(class        [mscorlib]System.Collections.Generic.List`1<!!0>,
                                                                   !!0,
                                                                   !!0,
                                                                   !!0,
                                                                   !!0,
                                                                   !!0,
                                                                   !!0,
                                                                   !!0,
                                                                   int32)
IL\u 002e:调用void-BoxingAndUnboxing.Program::TestList(class[mscorlib]System.Collections.Generic.List`1,
!!0,
!!0,
!!0,
!!0,
!!0,
!!0,
!!0,
int32)
以及功能定义的CIL代码:

.method private hidebysig static void  TestList<T>(class [mscorlib]System.Collections.Generic.List`1<!!T> list,
                                               !!T item,
                                               !!T t2,
                                               !!T t3,
                                               !!T t4,
                                               !!T t5,
                                               !!T t6,
                                               !!T t7,
                                               int32 t8) cil managed
.method private隐藏静态void TestList(class[mscorlib]System.Collections.Generic.List`1 List,
!!T项,
!!T t2,
!!T t3,
!!T t4,
!!T t5,
!!T t6,
!!T t7,
int32 t8)cil管理
以及显示类型为的CIL代码!0而不是!!0

 IL_001d:  callvirt   instance bool class [mscorlib]System.Collections.Generic.List`1<!!T>::Contains(!0)
il001d:callvirt实例bool类[mscorlib]System.Collections.Generic.List`1::Contains(!0)

!0
表示包含类的第一个泛型参数。
!!0
表示方法本身的第一个通用参数

类C{void M(xa,yb){}
中的方法将被称为
C`1::M`1(!0,!!0)
<代码>!0是
X
,并且
!!0
Y

最后,
!!引用方法时使用的0
,表示该方法声明的第一个参数,而
!!实际实现中使用的T
,指当前方法的类型参数,名为
T

!!0
在方法引用中是必要的,以区分
M(X X,Y)
M(Y Y,X)
,它们变成
M`2(!!0,!!1)
M`2(!!1,!!0)

 IL_001d:  callvirt   instance bool class [mscorlib]System.Collections.Generic.List`1<!!T>::Contains(!0)