OpCodes.Castclass 字段

定义

尝试强制转换通过对指定类的引用传递的对象。

public: static initonly System::Reflection::Emit::OpCode Castclass;
public static readonly 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
[<System.Runtime.InteropServices.ComVisible(true)>]
 staticval mutable Castclass : System.Reflection.Emit.OpCode
Public Shared ReadOnly Castclass As OpCode 

字段值

属性

注解

下表列出了指令的十六进制和Microsoft中间语言(MSIL)程序集格式,以及简短的参考摘要:

Format 程序集格式 Description
74 <T> castclass class 将对象强制转换为类型的 class新对象。

堆栈过渡行为按顺序排列,为:

  1. 对象引用被推送到堆栈上。

  2. 从堆栈中弹出对象引用;引用的对象被强制转换为指定的 class

  3. 如果成功,则会将新的对象引用推送到堆栈上。

指令 castclass 尝试将堆栈上的对象引用(类型 O)强制转换为指定的类。 新类由指示所需类的元数据标记指定。 如果堆栈顶部的对象类不实现新类(假设新类是接口),并且不是新类的派生类,则会引发一个 InvalidCastException 。 如果对象引用为 null 引用, castclass 则成功并将新对象作为 null 引用返回。

InvalidCastException 如果 obj 无法强制转换为类,将引发 。

TypeLoadException 如果找不到类,将引发 。 当Microsoft中间语言(MSIL)指令转换为本机代码而不是运行时时,通常会检测到这种情况。

以下 Emit 方法重载可以使用 castclass 操作码:

适用于