Format | Assembly Format | Description |
---|---|---|
A5 <T> | unbox.any typeTok |
Extract a value-type from obj, its boxed representation |
…, obj → …, value or obj
When applied to the boxed form of a value type, the unbox.any
instruction extracts the value contained within obj (of type O
). (It is equivalent to unbox
followed by ldobj
.) When applied to a reference type, the unbox.any
instruction has the same effect as castclass
typeTok.
If typeTok is a GenericParam, the runtime behavior is determined by the actual instantiation of that parameter.
System.InvalidCastException
is thrown if obj is not a boxed value type or a reference type, typeTok is Nullable<T>
and obj is not a boxed T
, or if the type of the value contained in obj is not verifier-assignable-to (§III.1.8.1.2.3) typeTok.
System.NullReferenceException
is thrown if obj is null and typeTok is a non-nullable value type (Partition I.8.2.4).
obj shall be of reference type and typeTok shall be a boxable type.
Verification tracks the type of value or obj as the intermediate type of typeTok.
There are two reasons for having both unbox.any
and unbox
instructions:
-
Unlike the
unbox
instruction, for value types,unbox.any
leaves a value, not an address of a value, on the stack. -
The type operand to
unbox
has a restriction: it can only represent value types and instantiations of generic value types.