ILGenerator.Emit Method (OpCode, Label)

May 11, 2014

Puts the specified instruction onto the Microsoft intermediate language (MSIL) stream and leaves space to include a label when fixes are done.

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

public virtual void Emit(
	OpCode opcode,
	Label label
)

Parameters

opcode
Type: System.Reflection.Emit.OpCode
The MSIL instruction to be emitted onto the stream.
label
Type: System.Reflection.Emit.Label
The label to branch to.

The instruction values are defined in the OpCodes enumeration.

Labels are created by using the DefineLabel method, and their location within the stream is fixed by using the MarkLabel method. If a single-byte instruction is used, the label can represent a jump of at most 127 bytes along the stream. opcode must represent a branch instruction. Because branches are relative instructions, label will be replaced with the correct offset to branch during the fixup process.

The following example demonstrates the use of the Emit(OpCode, Label) method overload to emit a conditional branch to a Label.

This code is part of a larger example provided for the BeginExceptionBlock method.


adderIL.BeginFinallyBlock();

// The finally block block displays a message on the TextBlock, if the 
// OutputBlock field has been set. Just as in the catch block, the following
// code tests whether the TextBlock is present, and skips the output if it is
// not present.
adderIL.Emit(OpCodes.Ldsfld, demoOutput);
adderIL.Emit(OpCodes.Ldnull);
adderIL.Emit(OpCodes.Ceq);
adderIL.Emit(OpCodes.Brtrue_S, skipOutputInFinally);

// Append a constant string to the Text property of the TextBlock.
adderIL.Emit(OpCodes.Ldsfld, demoOutput);
adderIL.Emit(OpCodes.Ldsfld, demoOutput);
adderIL.Emit(OpCodes.Callvirt, getter);
adderIL.Emit(OpCodes.Ldstr, "Executing the finally block.\n");
adderIL.Emit(OpCodes.Call, concat2);
adderIL.Emit(OpCodes.Callvirt, setter);

adderIL.MarkLabel(skipOutputInFinally);
// This is the end of the try/catch/finally block.
adderIL.EndExceptionBlock();


Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Windows Phone

Show:
© 2014 Microsoft