Bagikan melalui


DynamicMethod.GetILGenerator Method (Int32)

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

Returns a Microsoft intermediate language (MSIL) generator for the method with the specified MSIL stream size.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Function GetILGenerator ( _
    streamSize As Integer _
) As ILGenerator
[SecuritySafeCriticalAttribute]
public ILGenerator GetILGenerator(
    int streamSize
)

Parameters

  • streamSize
    Type: System.Int32
    The size of the MSIL stream, in bytes.

Return Value

Type: System.Reflection.Emit.ILGenerator
An ILGenerator object for the method, with the specified MSIL stream size.

Remarks

After a dynamic method has been completed, by calling the CreateDelegate or Invoke method, any further attempt to add MSIL is ignored. No exception is thrown.

Examples

The following code example demonstrates this method overload. This code example is part of a larger example provided for the DynamicMethod class.

' Create an array that specifies the parameter types of the
' overload of String.Format to be used in Hello.
Dim formatStringArgs() As Type = { GetType(String), GetType(Object), _
                           GetType(Object), GetType(Object) }
Dim formatString As MethodInfo = GetType(String). _
    GetMethod("Format", formatStringArgs)

' Get an ILGenerator and emit a body for the dynamic method,
' using a stream size larger than the IL that will be
' emitted.
Dim il As ILGenerator = hello.GetILGenerator(256)
' Push the format string for String.Format on the stack.
il.Emit(OpCodes.Ldstr, "Hello, {0}, {1} squared is {2}!")
' Load the first argument, which is a string, onto the stack,
' as the second argument of String.Format.
il.Emit(OpCodes.Ldarg_0)
' Load the second argument, and box it. This is the third argument
' of String.Format, and because it is a value type it must be boxed
' in order to pass it as an object.
il.Emit(OpCodes.Ldarg_1)
il.Emit(OpCodes.Box, GetType(Integer))
' Load the second argument twice more, as the arguments of the 
' multiply operation that will square it. 
il.Emit(OpCodes.Ldarg_1)
il.Emit(OpCodes.Ldarg_1)
' Multiply the last two arguments on the stack, leaving the 
' result on the stack. The result must be boxed, so it can be passed
' as the fourth argument of String.Format.
il.Emit(OpCodes.Mul)
il.Emit(OpCodes.Box, GetType(Integer))
' Call the overload of String.Format that prints a string, formatting
' it to include the next three objects on the stack.
il.Emit(OpCodes.Call, formatString)
' The Hello method returns the formatted string, which is already
' on the stack after the call to String.Format.
il.Emit(OpCodes.Ret)
// Create an array that specifies the parameter types of the
// overload of String.Format to be used in Hello.
Type[] formatStringArgs = { typeof(string), typeof(object), typeof(object), 
                           typeof(object) };
// Get the overload of String.Format that has one String 
// parameter for the format, and three object parameters.
MethodInfo formatString = typeof(String).GetMethod("Format",
    formatStringArgs);

// Get an ILGenerator and emit a body for the dynamic method,
// using a stream size larger than the IL that will be
// emitted.
ILGenerator il = hello.GetILGenerator(256);
// Push the format string for String.Format on the stack.
il.Emit(OpCodes.Ldstr, "Hello, {0}, {1} squared is {2}!");
// Load the first argument, which is a string, onto the stack,
// as the second argument of String.Format.
il.Emit(OpCodes.Ldarg_0);
// Load the second argument, and box it. This is the third argument
// of String.Format, and because it is a value type it must be boxed
// in order to pass it as an object.
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Box, typeof(int));
// Load the second argument twice more, as the arguments of the 
// multiply operation that will square it. 
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldarg_1);
// Multiply the last two arguments on the stack, leaving the 
// result on the stack. The result must be boxed, so it can be passed
// as the fourth argument of String.Format.
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Box, typeof(int));
// Call the overload of String.Format that prints a string, formatting
// it to include the next three objects on the stack.
il.Emit(OpCodes.Call, formatString);
// The Hello method returns the formatted string, which is already
// on the stack after the call to String.Format.
il.Emit(OpCodes.Ret);

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.