OpCodes.Box 字段


将值类转换为对象引用(O 类型)。Converts a value type to an object reference (type O).

public: static initonly System::Reflection::Emit::OpCode Box;
public static readonly System.Reflection.Emit.OpCode Box;
 staticval mutable Box : System.Reflection.Emit.OpCode
Public Shared ReadOnly Box As OpCode 



下表列出了指令的十六进制和 Microsoft 中间语言(MSIL)程序集格式以及简短的参考摘要: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 >8C < T > box valTypeTokenbox valTypeToken 将值类型(类型 @no__t 中指定的类型)转换为真正的对象引用。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. 从堆栈中弹出值类型;执行 box 操作。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.

值类型在公共语言基础结构(CLI)中具有两种不同的表示形式: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.

@No__t-0 指令将 "raw" (未装箱)值类型转换为对象引用(类型 @no__t 为-1)。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. @no__t 为一个元数据标记,该标记指示堆栈上值类型的类型。valTypeToken is a metadata token indicating the type of the value type on the stack.

如果内存不足,无法满足请求,则会引发 OutOfMemoryExceptionOutOfMemoryException is thrown if there is insufficient memory to satisfy the request.

如果找不到类,则会引发 TypeLoadExceptionTypeLoadException is thrown if the class cannot be found. 当 Microsoft 中间语言(MSIL)转换为本机代码而不是运行时,通常会检测到此情况。This is typically detected when Microsoft Intermediate Language (MSIL) is converted to native code, rather than at runtime.

以下 @no__t 0 方法重载可以使用 @no__t 操作码:The following Emit method overload can use the box opcode:

  • ILGenerator (OpCode,Type)ILGenerator.Emit(OpCode, Type)