Partager via


ILGenerator.Emit Method (OpCode, MethodInfo)

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

Puts the specified instruction onto the Microsoft intermediate language (MSIL) stream followed by the metadata token for the given method.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Overridable Sub Emit ( _
    opcode As OpCode, _
    meth As MethodInfo _
)
[SecuritySafeCriticalAttribute]
public virtual void Emit(
    OpCode opcode,
    MethodInfo meth
)

Parameters

Exceptions

Exception Condition
ArgumentNullException

meth is nulla null reference (Nothing in Visual Basic).

NotSupportedException

meth is a generic method for which the MethodInfo.IsGenericMethodDefinition property is false.

Remarks

The instruction values are defined in the OpCodes enumeration.

The location of meth is recorded so that the instruction stream can be patched if necessary when persisting the module to a portable executable (PE) file.

If meth represents a generic method, it must be a generic method definition. That is, its MethodInfo.IsGenericMethodDefinition property must be true.

Examples

The following example demonstrates the use of Emit method overloads to emit an instruction that requires a FieldInfo and an instruction that requires a MethodInfo. The first instruction loads an object from a static field (Shared field in Visual Basic), and the second instruction calls the get accessor for a property of the object.

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

adderIL.Emit(OpCodes.Ldsfld, demoOutput)
adderIL.Emit(OpCodes.Callvirt, getter)
adderIL.Emit(OpCodes.Ldsfld, demoOutput);
adderIL.Emit(OpCodes.Callvirt, getter);

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.