OpCodes.Constrained 字段


约束要对其进行虚方法调用的类型。Constrains the type on which a virtual method call is made.

public: static initonly System::Reflection::Emit::OpCode Constrained;
public static readonly System.Reflection.Emit.OpCode Constrained;
 staticval mutable Constrained : System.Reflection.Emit.OpCode
Public Shared ReadOnly Constrained 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
FE 16 < T >FE 16 < T > 有限.constrained. thisType 对被约束为类型 T的类型调用虚拟方法。Call a virtual method on a type constrained to be type T.

仅对 callvirt 指令允许 constrained 前缀。The constrained prefix is permitted only on a callvirt instruction.

此时 MSIL 堆栈的状态必须如下所示:The state of the MSIL stack at this point must be as follows:

  1. 托管指针 ptr被推送到堆栈上。A managed pointer, ptr, is pushed onto the stack. ptr 的类型必须是 thisType的托管指针(&)。The type of ptr must be a managed pointer (&) to thisType. 请注意,这与没有前缀 callvirt 指令的情况不同,后者需要 thisType的引用。Note that this is different from the case of an unprefixed callvirt instruction, which expects a reference of thisType.

  2. arg1 通过 argN 将方法参数推送到堆栈上,就像没有前缀的 callvirt 指令一样。Method arguments arg1 through argN are pushed onto the stack, just as with an unprefixed callvirt instruction.

constrained 前缀旨在允许 callvirt 指令以统一的方式进行,而不管 thisType 是值类型还是引用类型。The constrained prefix is designed to allow callvirt instructions to be made in a uniform way independent of whether thisType is a value type or a reference type.

如果 callvirt method 指令以 constrained thisType为前缀,则按如下方式执行指令:When a callvirt method instruction has been prefixed by constrained thisType, the instruction is executed as follows:

  • 如果 thisType 是引用类型(而不是值类型),则 ptr 被取消引用并作为 "this" 指针传递到 methodcallvirtIf thisType is a reference type (as opposed to a value type) then ptr is dereferenced and passed as the 'this' pointer to the callvirt of method.

  • 如果 thisType 是值类型,并且 thisType 实现 method 那么,ptr 将作为 "this" 指针传递到 call method 指令,以 method thisType的实现实现。If thisType is a value type and thisType implements method then ptr is passed unmodified as the 'this' pointer to a call method instruction, for the implementation of method by thisType.

  • 如果 thisType 是值类型,并且 thisType 未实现 method,则 ptr 是取消引用并装箱,并作为 "this" 指针传递到 callvirt method 指令。If thisType is a value type and thisType does not implement method then ptr is dereferenced, boxed, and passed as the 'this' pointer to the callvirt method instruction.

仅当在 ObjectValueTypeEnum 上定义 method,而不是 thisType重写时,才会出现这种情况。This last case can occur only when method was defined on Object, ValueType, or Enum and not overridden by thisType. 在这种情况下,装箱会导致生成原始对象的副本。In this case, the boxing causes a copy of the original object to be made. 但是,因为没有 ObjectValueTypeEnum 的方法修改对象的状态,所以无法检测到这种情况。However, because none of the methods of Object, ValueType, and Enum modify the state of the object, this fact cannot be detected.

constrained 前缀支持创建泛型代码的 IL 生成器。The constrained prefix supports IL generators that create generic code. 通常,callvirt 指令对值类型无效。Normally the callvirt instruction is not valid on value types. 相反,IL 编译器必须在编译时有效地执行上述 "this" 转换,具体取决于 ptr 的类型和所调用的方法。Instead it is required that IL compilers effectively perform the 'this' transformation outlined above at compile time, depending on the type of ptr and the method being called. 但是,如果 ptr 是在编译时未知的泛型类型,则不能在编译时进行此转换。However, when ptr is a generic type that is unknown at compile time, it is not possible to make this transformation at compile time.

constrained opcode 允许 IL 编译器以统一的方式对虚拟函数进行调用,而不管 ptr 是值类型还是引用类型。The constrained opcode allows IL compilers to make a call to a virtual function in a uniform way independent of whether ptr is a value type or a reference type. 尽管它适用于 thisType 为泛型类型变量的情况,但 constrained 前缀还适用于非泛型类型,并且可以降低以隐藏值类型和引用类型之间的区别的语言生成虚拟调用的复杂性。Although it is intended for the case where thisType is a generic type variable, the constrained prefix also works for nongeneric types and can reduce the complexity of generating virtual calls in languages that hide the distinction between value types and reference types.

使用 constrained 前缀还可以避免值类型出现潜在的版本管理问题。Using the constrained prefix also avoids potential versioning problems with value types. 如果未使用 constrained 前缀,则必须根据值类型是否覆盖 System.object 的方法来发出不同的 IL。If the constrained prefix is not used, different IL must be emitted depending on whether or not a value type overrides a method of System.Object. 例如,如果值类型 V 重写对象的 ToString ()方法,则会发出 call V.ToString() 指令;如果没有,则发出 box 指令和 callvirt Object.ToString() 指令。For example, if a value type V overrides the Object.ToString() method, a call V.ToString() instruction is emitted; if it does not, a box instruction and a callvirt Object.ToString() instruction are emitted. 如果稍后要删除重写,并且在后一种情况下,如果后来添加了替代,则可能会出现版本控制问题。A versioning problem can arise in the former case if the override is later removed, and in the latter case if an override is later added.

constrained 前缀还可用于对值类型调用接口方法,因为实现接口方法的值类型方法可以使用 MethodImpl进行更改。The constrained prefix can also be used for invocation of interface methods on value types, because the value type method implementing the interface method can be changed using a MethodImpl. 如果未使用 constrained 前缀,则强制编译器选择要在编译时绑定到的值类型的方法。If the constrained prefix is not used, the compiler is forced to choose which of the value type's methods to bind to at compile time. 使用 constrained 前缀允许 MSIL 绑定到在运行时(而不是在编译时)实现接口方法的方法。Using the constrained prefix allows the MSIL to bind to the method that implements the interface method at run time, rather than at compile time.

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

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