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

r00*_*dev 3 c# generics cil

为什么泛型类型在函数定义中看起来像(!! T),在CIL中看起来是(!! 0).在某些情况下,泛型类型的参数看起来像(!0).隐藏的秘密是什么?或者为什么这样设计?

先感谢您.

下面是C#中的代码:

    private static void Main(string[] args)
    {
        Test<int>();
        TestList<int>(new List<int>{ 1, 2,3 }, 1, 2, 3, 4, 5, 6, 7,8);
        Console.ReadKey();
    }

    private static void TestList<T>(List<T> list, T item, T t2, T t3, T t4, T t5, T t6, T t7, int t8)
    {
        Console.WriteLine(item == null);
        Console.WriteLine("Collection contains {0}? {1}",  item, list.Contains(item));
    }
Run Code Online (Sandbox Code Playgroud)

和调用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)
Run Code Online (Sandbox Code Playgroud)

和函数定义的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
Run Code Online (Sandbox Code Playgroud)

和显示类型的CIL代码为!0而不是!! 0

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

SLa*_*aks 6

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

该方法class C<X> { void M<Y>(X a, Y b) {} }将被称为C`1::M`1(!0, !!0).!0是的X,!!0是的Y.

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

!!0在方法参考中有必要区分M<X, Y>(X x, Y y)M<X, Y>(Y y, X x)成为M`2(!!0, !!1)M`2(!!1, !!0).