Emit Method (OpCode, LocalBuilder)

ILGenerator.Emit Method (OpCode, LocalBuilder)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Puts the specified instruction onto the Microsoft intermediate language (MSIL) stream followed by the index of the given local variable.

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

public virtual void Emit(
	OpCode opcode,
	LocalBuilder local


Type: System.Reflection.Emit.OpCode
The MSIL instruction to be emitted onto the stream.
Type: System.Reflection.Emit.LocalBuilder
A local variable.


The parent method of the local parameter does not match the method associated with this ILGenerator.


local is null.


opcode is a single-byte instruction, and local represents a local variable with an index greater than Byte.MaxValue.

The instruction values are defined in the OpCodes enumeration.

The following example demonstrates the use of Emit method overloads to emit an instruction with no target, an instruction that requires a LocalBuilder, and an instruction that requires a Label. This code is part of a larger example provided for the BeginExceptionBlock method.

// The addition failed, but the function has to return an integer value, so
// store -1 in the local variable named result. Use the Leave instruction to
// exit the catch block. The finally block will be executed.
adderIL.Emit(OpCodes.Stloc, result);
adderIL.Emit(OpCodes.Leave_S, exTryCatchFinally);

Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Windows Phone

© 2016 Microsoft