InstructionEncoder 구조체

정의

CIL(공용 중간 언어) 지침을 인코딩합니다.

public value class InstructionEncoder
public readonly struct InstructionEncoder
public struct InstructionEncoder
type InstructionEncoder = struct
Public Structure InstructionEncoder
상속
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 하세요.

생성자

Name Description
InstructionEncoder(BlobBuilder, ControlFlowBuilder)

코드 및 제어 흐름 작성기에서 지원되는 인코더를 만듭니다.

속성

Name Description
CodeBuilder

인코딩된 명령이 기록되는 기본 작성기입니다.

ControlFlowBuilder

작성기 추적 레이블, 분기 및 예외 처리기입니다.

Offset

인코딩된 다음 명령의 오프셋입니다.

메서드

Name Description
Branch(ILOpCode, LabelHandle)

분기 명령을 인코딩합니다.

Call(EntityHandle)

명령 및 해당 피연산자를 call 인코딩합니다.

Call(MemberReferenceHandle)

명령 및 해당 피연산자를 call 인코딩합니다.

Call(MethodDefinitionHandle)

명령 및 해당 피연산자를 call 인코딩합니다.

Call(MethodSpecificationHandle)

명령 및 해당 피연산자를 call 인코딩합니다.

CallIndirect(StandaloneSignatureHandle)

명령 및 해당 피연산자를 calli 인코딩합니다.

DefineLabel()

나중에 명령 스트림의 위치를 표시하고 참조하는 데 사용할 수 있는 레이블을 정의합니다.

LoadArgument(Int32)

인수 로드 명령을 인코딩합니다.

LoadArgumentAddress(Int32)

인수 주소 로드 명령을 인코딩합니다.

LoadConstantI4(Int32)

상수 로드 명령을 인코딩합니다 Int32 .

LoadConstantI8(Int64)

상수 로드 명령을 인코딩합니다 Int64 .

LoadConstantR4(Single)

상수 로드 명령을 인코딩합니다 Single .

LoadConstantR8(Double)

상수 로드 명령을 인코딩합니다 Double .

LoadLocal(Int32)

지역 변수 로드 명령을 인코딩합니다.

LoadLocalAddress(Int32)

지역 변수 주소 로드 명령을 인코딩합니다.

LoadString(UserStringHandle)

명령 및 해당 피연산자를 ldstr 인코딩합니다.

MarkLabel(LabelHandle)

지정된 레이블을 현재 IL 오프셋과 연결합니다.

OpCode(ILOpCode)

지정된 op-code를 인코딩합니다.

StoreArgument(Int32)

인수 저장소 명령을 인코딩합니다.

StoreLocal(Int32)

지역 변수 저장소 명령을 인코딩합니다.

Switch(Int32)

스위치 명령 인코딩을 시작합니다.

Token(EntityHandle)

토큰을 인코딩합니다.

Token(Int32)

토큰을 인코딩합니다.

적용 대상