OpCodes.Stelem_Ref 字段


用计算堆栈上的对象 ref 值(O 类型)替换给定索引处的数组元素。Replaces the array element at a given index with the object ref value (type O) on the evaluation stack.

public: static initonly System::Reflection::Emit::OpCode Stelem_Ref;
public static readonly System.Reflection.Emit.OpCode Stelem_Ref;
 staticval mutable Stelem_Ref : System.Reflection.Emit.OpCode
Public Shared ReadOnly Stelem_Ref 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
A2A2 stelem.refstelem.ref 将所提供索引处的数组元素替换为堆栈上的 ref 值(类型 O)。Replaces an array element at the supplied index with the ref value (type O) on the stack.

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

  1. 对数组 array 的对象引用被推送到堆栈上。An object reference to an array, array, is pushed onto the stack.

  2. array 中的元素的有效索引推送到堆栈上。A valid index to an element in array is pushed onto the stack.

  3. 将值推送到堆栈上。A value is pushed onto the stack.

  4. 从堆栈中弹出值、索引和数组引用;将值放入给定索引处的数组元素。The value, the index, and the array reference are popped from the stack; the value is put into the array element at the given index.

@No__t-0 指令用推送到堆栈上的 ref (类型 @no__t)值替换一维 @no__t 数组中提供的索引处的元素的值。The stelem.ref instruction replaces the value of the element at the supplied index in the one-dimensional array array with the ref (type O) value pushed onto the stack.

数组是对象,因此由类型 O 的值表示。Arrays are objects and hence represented by a value of type O. 索引的类型为 native intThe index is type native int.

请注意,在为数组元素赋值之前,stelem.ref 会隐式将提供的值强制转换为 array 的元素类型。Note that stelem.ref implicitly casts the supplied value to the element type of array before assigning the value to the array element. 此强制转换可能会失败,即使对于已验证的代码也是如此。This cast can fail, even for verified code. 因此,stelem.ref 指令可能会引发 @no__t 1。Thus the stelem.ref instruction can throw InvalidCastException. 对于不是从零开始的一维数组和多维数组,@no__t 0 类提供 @no__t 的方法。For one-dimensional arrays that aren't zero-based and for multidimensional arrays, the Array class provides a SetValue method.

如果 array 为 null 引用,则会引发 NullReferenceExceptionNullReferenceException is thrown if array is a null reference.

如果 index 为负数或大于 array 的界限,则会引发 IndexOutOfRangeExceptionIndexOutOfRangeException is thrown if index is negative, or larger than the bound of array.

如果 array 不包含所需类型的元素,则会引发 ArrayTypeMismatchExceptionArrayTypeMismatchException is thrown if array does not hold elements of the required type.

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

  • ILGenerator.Emit(OpCode)ILGenerator.Emit(OpCode)