This project is read-only.

The ILGenerator Object

CCI Metadata

The Microsoft.Cci.ILGenerator object simplifies the task of creating MSIL instructions for method bodies. The following example creates a new ILGenerator object.
ILGenerator ilGenerator = new ILGenerator(host);

Notice that you must pass the host object to ILGenerator; it depends on the host for application policy settings.

Create the Method Body

The MSIL for a method is contained in a method body. The Microsoft.Cci.ILGeneratorMethodBody object represents a method body. The following example from the HelloIL sample creates the Main method body.
ILGeneratorMethodBody body = new ILGeneratorMethodBody(ilGenerator, true, 1);
body.MethodDefinition = mainMethod;
mainMethod.Body = body;

The ILGeneratorMethodBody constructor has three parameters:
  1. An ILGenerator object, which ILGeneratorMethodBody uses to construct the method body.
  2. A Boolean value that specifies whether to zero the stack on method entry. Set this parameter to true to zero the stack.
  3. The maximum number of elements on the evaluation stack during method execution.
The last two lines in the example establish a link between the Main method body and its metadata by assigning the Main method’s MethodDefinition object to ILGeneratorMethodBody.MethodDefinition, and assigning the ILGeneratorMethodBody object to MethodDefinition.Body.

Create Type and Method Definitions

To create the MSIL for a method, you must create definition objects for the members that are called and their types. CCI Metadata includes helper objects that create an appropriate definition object and return its immutable representation. The following examples create type and method definitions for the .NET System.Console.WriteLine method.
The following code creates a type definition for System.Console.
INamedTypeDefinition systemConsole = UnitHelper.FindType(nameTable,

Microsoft.Cci.UnitHelper.FindType creates a NamedTypeDefinition object for the method’s type and returns the object’s Microsoft.Cci.INamedTypeDefinition interface. FindType has three parameters:
  1. The NameTable object.
  2. The assembly that contains the method, which is the .NET core assembly in this case.
  3. The type name.
The following example creates a method definition for WriteLine.
IMethodDefinition writeLine = TypeHelper.GetMethod(systemConsole,

Microsoft.Cci.TypeHelper.GetMethod has three parameters:
  1. The type definition from the previous example.
  2. An IName interface for the method name, created by calling NameTable.GetNameFor.
  3. A list of the method’s parameter types, in order. In this case, the method has a single string parameter.

Add MSIL to the Method Body

The final step is to add MSIL to the method body. The following example adds WriteLine(“hello”):
ilGenerator.Emit(OperationCode.Ldstr, "hello");
ilGenerator.Emit(OperationCode.Call, writeLine);

The ILGenerator.Emit method inserts the specified instruction into the method body. Microsoft.Cci.OperationCode is an enumeration that represents all CLI Common Intermediate Language operation codes.

Last edited Dec 18, 2009 at 6:56 PM by Guy_Smith, version 6


No comments yet.