InstructionEncoder 结构 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
对公共中间语言 (CIL) 指令进行编码。
public value class InstructionEncoderpublic readonly struct InstructionEncoderpublic struct InstructionEncodertype InstructionEncoder = structPublic Structure InstructionEncoder- 继承
示例
此示例演示如何使用 InstructionEncoder发出方法体:
// The following code emits a method body similar to this C# code:
/*public static double CalcRectangleArea(double length, double width)
{
    if (length < 0.0)
    {
        throw new ArgumentOutOfRangeException("length");
    }
    if (width < 0.0)
    {
        throw new ArgumentOutOfRangeException("width");
    }
    return length * width;
}*/
private static InstructionEncoder EmitMethodBody(MetadataBuilder metadata, AssemblyReferenceHandle corlibAssemblyRef)
{
    var codeBuilder = new BlobBuilder();
    var encoder = new InstructionEncoder(codeBuilder, new ControlFlowBuilder());
    // Get a reference to the System.ArgumentOutOfRangeException type
    TypeReferenceHandle typeRefHandle = metadata.AddTypeReference(
    corlibAssemblyRef,
    metadata.GetOrAddString("System"),
    metadata.GetOrAddString("ArgumentOutOfRangeException"));
    // Signature: .ctor(string)
    var ctorSignature = new BlobBuilder();
    new BlobEncoder(ctorSignature).
        MethodSignature(isInstanceMethod: true).
        Parameters(1, returnType => returnType.Void(), parameters => parameters.AddParameter().Type().String());
    BlobHandle ctorBlobIndex = metadata.GetOrAddBlob(ctorSignature);
    // Get a reference to the System.ArgumentOutOfRangeException constructor
    MemberReferenceHandle ctorMemberRef = metadata.AddMemberReference(
        typeRefHandle,
        metadata.GetOrAddString(".ctor"),
        ctorBlobIndex);
    LabelHandle label1 = encoder.DefineLabel();
    LabelHandle label2 = encoder.DefineLabel();
    // ldarg.0
    encoder.OpCode(ILOpCode.Ldarg_0);
    // ldc.r8 0
    encoder.LoadConstantR8(0);
    // bge.un.s LABEL1
    encoder.Branch(ILOpCode.Bge_un_s, label1);
    // ldstr "length"
    encoder.LoadString(metadata.GetOrAddUserString("length"));
    // newobj instance void [System.Runtime]System.ArgumentOutOfRangeException::.ctor(string)
    encoder.OpCode(ILOpCode.Newobj);
    encoder.Token(ctorMemberRef);
    // throw
    encoder.OpCode(ILOpCode.Throw);
    // LABEL1: ldarg.1
    encoder.MarkLabel(label1);
    encoder.OpCode(ILOpCode.Ldarg_1);
    // ldc.r8 0
    encoder.LoadConstantR8(0);
    // bge.un.s LABEL2
    encoder.Branch(ILOpCode.Bge_un_s, label2);
    // ldstr "width"
    encoder.LoadString(metadata.GetOrAddUserString("width"));
    // newobj instance void [System.Runtime]System.ArgumentOutOfRangeException::.ctor(string)
    encoder.OpCode(ILOpCode.Newobj);
    encoder.Token(ctorMemberRef);
    // throw
    encoder.OpCode(ILOpCode.Throw);
    // LABEL2: ldarg.0
    encoder.MarkLabel(label2);
    encoder.OpCode(ILOpCode.Ldarg_0);
    // ldarg.1
    encoder.OpCode(ILOpCode.Ldarg_1);
    // mul
    encoder.OpCode(ILOpCode.Mul);
    // ret
    encoder.OpCode(ILOpCode.Ret);
    return encoder;
}
注解
类 InstructionEncoder 用于发出构成方法主体的 CIL 指令。 有关发出方法的完整示例,请参阅 MetadataBuilder 类文档。
构造函数
| InstructionEncoder(BlobBuilder, ControlFlowBuilder) | 创建由代码和控制流生成器支持的编码器。 | 
属性
| CodeBuilder | 已编码的指令写入到的基础生成器。 | 
| ControlFlowBuilder | 生成器跟踪标签、分支和异常处理程序。 | 
| Offset | 下一个已编码的指令的偏移量。 | 
方法
| Branch(ILOpCode, LabelHandle) | 对分支指令进行编码。 | 
| Call(EntityHandle) | 对  | 
| Call(MemberReferenceHandle) | 对  | 
| Call(MethodDefinitionHandle) | 对  | 
| Call(MethodSpecificationHandle) | 对  | 
| CallIndirect(StandaloneSignatureHandle) | 对  | 
| DefineLabel() | 定义一个标签,稍后可用它来标记和引用指令流中的位置。 | 
| LoadArgument(Int32) | 对参数加载指令进行编码。 | 
| LoadArgumentAddress(Int32) | 对参数地址加载指令进行编码。 | 
| LoadConstantI4(Int32) | 对 Int32 常量加载指令进行编码。 | 
| LoadConstantI8(Int64) | 对 Int64 常量加载指令进行编码。 | 
| LoadConstantR4(Single) | 对 Single 常量加载指令进行编码。 | 
| LoadConstantR8(Double) | 对 Double 常量加载指令进行编码。 | 
| LoadLocal(Int32) | 对局部变量加载指令进行编码。 | 
| LoadLocalAddress(Int32) | 对局部变量地址加载指令进行编码。 | 
| LoadString(UserStringHandle) | 对  | 
| MarkLabel(LabelHandle) | 将指定的标签与当前 IL 偏移量关联。 | 
| OpCode(ILOpCode) | 对指定的操作代码进行编码。 | 
| StoreArgument(Int32) | 对参数存储指令进行编码。 | 
| StoreLocal(Int32) | 对局部变量存储指令进行编码。 | 
| Switch(Int32) | 开始对开关指令进行编码。 | 
| Token(EntityHandle) | 将令牌进行编码。 | 
| Token(Int32) | 将令牌进行编码。 |