OpCodes.Castclass 字段

定义

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

public: static initonly System::Reflection::Emit::OpCode Castclass;
[System.Runtime.InteropServices.ComVisible(true)]
public static readonly System.Reflection.Emit.OpCode Castclass;
 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 将对象强制转换为类型 @no__t 的新对象。Casts 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.

@No__t-0 指令尝试将堆栈顶部的对象引用(类型 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. 如果堆栈顶部对象的类未实现新类(假定新类为接口),并且不是新类的派生类,则会引发 InvalidCastExceptionIf 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.

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

如果找不到类,则会引发 TypeLoadExceptionTypeLoadException 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.

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

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

适用于