The ECMA Common Language Infrastructure documentation says this about the CIL “isinst class” instruction:
Correct CIL ensures that class is a valid typeref or typedef or typespec token indicating a class, and
that obj is always either null or an object reference.
This implies that a valuetype is not allowed, right? But mscorlib.dll contains a method System.RuntimeTypeHandle::Equals(object obj) with the following instruction:
IL_0001: isinst System.RuntimeTypeHandle
And System.RuntimeTypeHandle is a valuetype. Can anybody put me right here?
Have a look at the declaration of
RuntimeTypeHandle:Although
RuntimeTypeHandleis declared as a struct its representation in CIL is some kind of special class. In other words, you can imagine structs as special classes that inherit fromSystem.ValueTypeand whose attributes follow a strict order.With that in mind
isinstwould be callable withRuntimeTypeHandle. For what I interpretisinstis not limited to reference types at all as long as there is a class representing the type.Let’s say we write in C#:
We get a compiler warning
What happens? We assign
4toi.ibecoms anint. On the next lineiis being auto-boxed to its correspondingReferenceType(class), so that the warning is obvious. We could even writeI hope this can contribute to some kind of clearification on this topic.