Export (0) Print
Expand All
Expand Minimize

OpCodes.Box Field

Converts a value type to an object reference (type O).

Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)

public static readonly OpCode Box
public static final OpCode Box
public static final var Box : OpCode

The following table lists the instruction's hexadecimal and Microsoft Intermediate Language (MSIL) assembly format, along with a brief reference summary:

Format

Assembly Format

Description

8C < T >

box valTypeToken

Convert a value type (of the type specified in valTypeToken) to a true object reference.

The stack transitional behavior, in sequential order, is:

  1. A value type is pushed onto the stack.

  2. The value type is popped from the stack; the box operation is performed.

  3. An object reference to the resulting "boxed" value type is pushed onto the stack.

A value type has two separate representations within the Common Language Infrastructure (CLI):

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

  • 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 box instruction converts the 'raw' (unboxed) value type into an object reference (type O). This is accomplished by creating a new object and copying the data from the value type into the newly allocated object. valTypeToken is a metadata token indicating the type of the value type on the stack.

OutOfMemoryException is thrown if there is insufficient memory to satisfy the request.

TypeLoadException is thrown if the class cannot be found. This is typically detected when Microsoft Intermediate Language (MSIL) is converted to native code, rather than at runtime.

The following Emit method overload can use the box opcode:

  • ILGenerator.Emit(OpCode, Type)

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft