Bagikan melalui


ILGenerator.Emit Method (OpCode)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Puts the specified instruction onto the stream of instructions.

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

Syntax

'Declaration
Public Overridable Sub Emit ( _
    opcode As OpCode _
)
public virtual void Emit(
    OpCode opcode
)

Parameters

Remarks

If the opcode parameter requires an argument, the caller must ensure that the argument length matches the length of the declared parameter. Otherwise, results will be unpredictable. For example, if the Emit instruction requires a 2-byte operand and the caller supplies a 4-byte operand, the runtime will emit two additional bytes to the instruction stream. These extra bytes will be Nop instructions.

The instruction values are defined in OpCodes.

Examples

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.Ldc_I4_M1)
adderIL.Emit(OpCodes.Stloc, result)
adderIL.Emit(OpCodes.Leave_S, exTryCatchFinally)
// 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.Ldc_I4_M1);
adderIL.Emit(OpCodes.Stloc, result);
adderIL.Emit(OpCodes.Leave_S, exTryCatchFinally);

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.