Skip to content

Latest commit

 

History

History
37 lines (20 loc) · 1.73 KB

iii.4.33-unbox-any.md

File metadata and controls

37 lines (20 loc) · 1.73 KB

III.4.33 unbox.any – convert boxed type to value

Format Assembly Format Description
A5 <T> unbox.any typeTok Extract a value-type from obj, its boxed representation

Stack Transition:

…, obj → …, value or obj

Description:

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.

Exceptions:

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-toIII.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).

Correctness:

obj shall be of reference type and typeTok shall be a boxable type.

Verifiability:

Verification tracks the type of value or obj as the intermediate type of typeTok.

Rationale:

There are two reasons for having both unbox.any and unbox instructions:

  1. Unlike the unbox instruction, for value types, unbox.any leaves a value, not an address of a value, on the stack.

  2. The type operand to unbox has a restriction: it can only represent value types and instantiations of generic value types.