tags:

views:

31

answers:

1

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?

+1  A: 

Have a look at the declaration of RuntimeTypeHandle:

.class public sequential ansi serializable sealed beforefieldinit RuntimeTypeHandle
    extends     System.ValueType
    implements  System.Runtime.Serialization.ISerializable

Although RuntimeTypeHandle is 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 from System.ValueType and whose attributes follow a strict order.

With that in mind isinst would be callable with RuntimeTypeHandle. For what I interpret isinst is not limited to reference types at all as long as there is a class representing the type.

Let's say we write in C#:

var i = 4;
var b = i is Int32;

We get a compiler warning

Warning: The given expression is always of the provided ('int') type.

What happens? We assign 4 to i. ibecoms an int. On the next line iis being auto-boxed to its corresponding ReferenceType (class), so that the warning is obvious. We could even write

var b = i is int;

I hope this can contribute to some kind of clearification on this topic.

Andreas
In other words, "isinst <valuetype>" really means "isinst <boxed valuetype>"?
TonyK
@TonyK - I believe that is correct - note that your cited portion of the spec indicates that _obj_ must be null or an object reference, so it wouldn't make sense for it to be an unboxed value type.
kvb
Actually, `is int` is translated to `isinst int32`. In Reflector `int32` can be clicked and references to `System.Int32`. The same with other value types like `bool`.
Andreas