OpCodes.Newobj 字段

定义

创建一个值类型的新对象或新实例,并将对象引用(O 类型)推送到计算堆栈上。Creates a new object or a new instance of a value type, pushing an object reference (type O) onto the evaluation stack.

public: static initonly System::Reflection::Emit::OpCode Newobj;
public static readonly System.Reflection.Emit.OpCode Newobj;
 staticval mutable Newobj : System.Reflection.Emit.OpCode
Public Shared ReadOnly Newobj 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
73 < T >73 < T > newobj ctornewobj ctor 分配未初始化的对象或值类型,并调用构造函数方法 ctorAllocates an uninitialized object or value type and calls the constructor method ctor.

堆栈转换行为顺序如下:The stack transitional behavior, in sequential order, is:

  1. 参数 arg1argn 按顺序推送到堆栈上。Arguments arg1 through argn are pushed on the stack in sequence.

  2. 从堆栈中弹出 argn 到 @no__t 的参数,并将其传递给 ctor 来创建对象。Arguments argn through arg1 are popped from the stack and passed to ctor for object creation.

  3. 对新的对象的引用被推送到堆栈上。A reference to the new object is pushed onto the stack.

@No__t-0 指令创建新的对象或值类型的新实例。The newobj instruction creates a new object or a new instance of a value type. @no__t 为元数据标记(必须标记为构造函数的 @no__t 1 或 @no__t 2),指示要调用的构造函数的名称、类和签名。Ctor is a metadata token (a methodref or methoddef that must be marked as a constructor) that indicates the name, class and signature of the constructor to call.

@No__t-0 指令分配与 ctor 关联的类的新实例,并根据需要将新实例中的所有字段初始化为0(正确的类型)或 null 引用。The newobj instruction allocates a new instance of the class associated with ctor and initializes all the fields in the new instance to 0 (of the proper type) or null references as appropriate. 然后,它调用具有给定参数的构造函数 ctor 以及新创建的实例。It then calls the constructor ctor with the given arguments along with the newly created instance. 调用构造函数后,即会将初始化的对象引用(类型 O)推送到堆栈上。After the constructor has been called, the now initialized object reference (type O) is pushed on the stack.

从构造函数的角度来看,未初始化的对象是参数0,传递给 newobj 的其他参数按照顺序排列。From the constructor's point of view, the uninitialized object is argument 0 and the other arguments passed to newobj follow in order.

所有从零开始的一维数组均使用 Newarr 而不是 newobj 创建。All zero-based, one-dimensional arrays are created using Newarr, not newobj. 另一方面,将使用 newobj 创建所有其他数组(多个维度,或一维但非从零开始)。On the other hand, all other arrays (more than one dimension, or one-dimensional but not zero-based) are created using newobj.

值类型通常不使用 @no__t 创建。Value types are not usually created using newobj. 它们通常作为参数或局部变量分配,使用 @no__t 0 (对于从零开始的一维数组)或作为对象的字段。They are usually allocated either as arguments or local variables, using newarr (for zero-based, one-dimensional arrays), or as fields of objects. 分配后,将使用 Initobj 对其进行初始化。Once allocated, they are initialized using Initobj. 但是,可以使用 newobj 指令在堆栈上创建值类型的新实例,然后可以将该实例作为参数进行传递,并将其存储在本地,依此类推。However, the newobj instruction can be used to create a new instance of a value type on the stack, that can then be passed as an argument, stored in a local, and so on.

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

如果找不到具有指定名称、类和签名的构造函数方法 ctor,则会引发 MissingMethodExceptionMissingMethodException is thrown if a constructor method ctor with the indicated name, class and signature could not be found. 当 Microsoft 中间语言(MSIL)指令转换为本机代码而不是运行时,通常会检测到此情况。This is typically detected when Microsoft Intermediate Language (MSIL) instructions are converted to native code, rather than at runtime.

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

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

适用于