DynamicMethod.GetILGenerator Method

Returns a Microsoft intermediate language (MSIL) generator for the method with a default MSIL stream size of 64 bytes.

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

public ILGenerator GetILGenerator()

Return Value

Type: System.Reflection.Emit.ILGenerator
An ILGenerator object for the method.

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.

The following example shows how to use the GetILGenerator method to emit the body for a simple dynamic method. The method has two parameters, a format string and an integer. The method calls the String.Format(String, Object) method overload with these two arguments, and returns the result.

using System.Reflection.Emit;
using System.Reflection;
using System;

public class Example
   // Declare a delegate that can be used to execute the dynamic method.
   private delegate string Caller(string msg, int number);

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      // Create an array that specifies the types of the parameters
      // of the dynamic method. This method has a String parameter
      // and an Integer parameter.
      Type[] paramTypes = { typeof(string), typeof(int) };

      // Create an unnamed dynamic method with a return type of
      // Integer and with two parameters whose types are specified by
      // the array paramTypes. The dynamic method is anonymously
      // hosted. 
      DynamicMethod method = new DynamicMethod("", typeof(string), paramTypes);

      // Get a MethodInfo for the overload of String.Format that
      // takes a format string and an object for insertion in the format 
      // string. The dynamic method uses this overload to format the 
      // return string.
      MethodInfo stringFormat = typeof(string).GetMethod("Format", 
         new Type[] { typeof(string), typeof(object) });

      // Get an ILGenerator and emit a body for the dynamic method.
      ILGenerator il = method.GetILGenerator();

      // Load the arguments onto the execution stack. The second 
      // argument is an Integer, so it must be boxed before it can be 
      // passed to a parameter of type Object. 
      il.Emit(OpCodes.Box, typeof(int));

      // Call the String.Format method. The return value from that call 
      // is placed on the execution stack, so the dynamic method simply 
      // returns.         
      il.Emit(OpCodes.Call, stringFormat);

      // Create a delegate that represents the dynamic method. This
      // action completes the dynamic method, and any further attempts
      // to change the method have no effect.
      Caller callMethod = (Caller) method.CreateDelegate(typeof(Caller));

      // Invoke the delegate and display the result.
      string ret = callMethod("The second argument is: {0}.", 1969);

      outputBlock.Text += 
         String.Format("Invoking the delegate returned the string '{0}'.", ret);

/* This code example produces the following output:

Invoking the delegate returned the string 'The second argument is: 1969.'.


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

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