OpCodes.Castclass 字段


尝试将引用传递的对象转换为指定的类。Attempts to cast an object passed by reference to the specified class.

public: static initonly System::Reflection::Emit::OpCode Castclass;
public static readonly System.Reflection.Emit.OpCode Castclass;
public static readonly System.Reflection.Emit.OpCode Castclass;
 staticval mutable Castclass : System.Reflection.Emit.OpCode
 staticval mutable Castclass : System.Reflection.Emit.OpCode
Public Shared ReadOnly Castclass 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
74 < T >74 < T > castclass classcastclass class 将对象强制转换为类型的新对象 classCasts an object to a new object of type class.

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

  1. 对象引用被推送到堆栈上。An object reference is pushed onto the stack.

  2. 从堆栈中弹出对象引用;被引用对象被强制转换为指定的 classThe object reference is popped from the stack; the referenced object is cast as the specified class.

  3. 如果成功,则将新的对象引用推送到堆栈上。If successful, a new object reference is pushed onto the stack.

castclass指令尝试将堆栈顶部) (类型的对象引用强制转换 O 为指定的类。The castclass instruction attempts to cast the object reference (type O) atop the stack to a specified class. 新类由指示所需类的元数据标记指定。The new class is specified by a metadata token indicating the desired class. 如果堆栈顶部对象的类未实现新类 (假设新类是接口) 并且不是新类的派生类,则 InvalidCastException 会引发。If the class of the object on the top of the stack does not implement the new class (assuming the new class is an interface) and is not a derived class of the new class then an InvalidCastException is thrown. 如果对象引用为空引用,则将 castclass 成功,并返回新的对象作为空引用。If the object reference is a null reference, castclass succeeds and returns the new object as a null reference.

InvalidCastException 如果无法将 obj 强制转换为类,则会引发。InvalidCastException is thrown if obj cannot be cast to class.

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

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