what rules exist for when !T is used vs !0?

I'm writing an IL static analysis tool, and I'm having a hard time understanding the rules governing how generic type parameters are referenced:

take this IL (from the IList<T> interface):

.property instance !T Item(
    int32 index
)
{
    .get instance !0 System.Collections.Generic.IList`1::get_Item(int32)
    .set instance void System.Collections.Generic.IList`1::set_Item(int32, !0)
}

Why is the !0 there instead of !T ? I presume they're equivalent as far as the VM goes, it just seems odd to use positional references at all when you're guaranteed to have the names.

Update: an additional case, from KeyedCollection.ctor:

IL_0037:  newobj instance void class System.Collections.Generic.Dictionary`2<!TKey,!TItem>::'.ctor'(class System.Collections.Generic.IEqualityComparer`1<!0>)
IL_003c:  stfld class System.Collections.Generic.Dictionary`2<!0,!1> class System.Collections.ObjectModel.KeyedCollection`2<!0,!1>::dictionary

In the Common Language Infrastructure standard, Partition II - Metadata and File Format, clause 7.1 "Types" it states:

Type ::=        Description
--------        -----------
'!'             Generic parameter in a type definition, accessed by index from 0

So short answer: because it's in the spec.

Long answer: This is speculation on my part, but basically, most IL commands are stack-based and use positional references as parameters all the time. That said, it makes sense that positional references are used for generics, in order to maintain common patterns/usage mechanisms in IL.

链接地址: http://www.djcxy.com/p/61548.html

上一篇: 如果由于程序退出/主窗体关闭而终止,表单不能正确关闭

下一篇: 什么时候存在什么规则!T被使用vs!0?