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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With