Skip to content

Latest commit

 

History

History
39 lines (21 loc) · 2.75 KB

iii.4.32-unbox.md

File metadata and controls

39 lines (21 loc) · 2.75 KB

III.4.32 unbox – convert boxed value type to its raw form

Format Assembly Format Description
79 <T> unbox valuetype Extract a value-type from obj, its boxed representation.

Stack Transition:

…, obj → …, valueTypePtr

Description:

A value type has two separate representations (see Partition I) within the CLI:

  • A 'raw' form used when a value type is embedded within another object.

  • A 'boxed' form, where the data in the value type is wrapped (boxed) into an object, so it can exist as an independent entity.

The unbox instruction converts obj (of type O), the boxed representation of a value type, to valueTypePtr (a controlled-mutability managed pointer (§III.1.8.1.2.2), type &), its unboxed form. valuetype is a metadata token (a typeref, typedef or typespec). The type of valuetype contained within obj must be verifier-assignable-to valuetype.

Unlike box, which is required to make a copy of a value type for use in the object, unbox is not required to copy the value type from the object. Typically it simply computes the address of the value type that is already present inside of the boxed object.

[Note: Typically, unbox simply computes the address of the value type that is already present inside of the boxed object. This approach is not possible when unboxing nullable value types. Because Nullable<T> values are converted to boxed Ts during the box operation, an implementation often must manufacture a new Nullable<T> on the heap and compute the address to the newly allocated object. end note]

Exceptions:

System.InvalidCastException is thrown if obj is not a boxed value type, valuetype is a 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) valuetype.

System.NullReferenceException is thrown if obj is null and valuetype is a non-nullable value type (Partition I.8.2.4).

System.TypeLoadException is thrown if the class cannot be found. (This is typically detected when CIL is converted to native code rather than at runtime.)

Correctness:

Correct CIL ensures that valueType is a typeref, typedef or typespec metadata token for some boxable value type, and that obj is always an object reference (i.e., of type O). If valuetype is the type Nullable<T>, the boxed instance shall be of type T.

Verifiability:

Verification requires that the type of valuetype contained within obj must be verifier-assignable-to valuetype