Add Code to the Method Bodies
To this point, HelloIL has been working entirely with metadata. To complete the assembly, you must add some code. CCI Metadata adds code to the assembly as a flat list of MSIL instruction, which is usually suitable only for creating short simple code blocks.
Another approach is to use the CCI Code Model API
, which is easier to use if you want generate large or complex code blocks.
The complete HelloIL Main method is:
.method public static void Main() cil managed
L_0000: ldstr "hello"
L_0005: call void [mscorlib]System.Console::WriteLine(string)
object handles the task of creating a method body and adding MSIL.
The following example from HelloIL creates a new ILGenerator
ILGenerator ilGenerator = new ILGenerator(host);
You must pass the host object to ILGenerator
, which depends on the host for application policy information.
For details, see
The ILGenerator Object
Create the Method Body
Method bodies are represented by Microsoft.Cci.ILGeneratorMethodBody
objects. The following example shows how HelloIL creates the Main method body.
ILGeneratorMethodBody body = new ILGeneratorMethodBody(ilGenerator, true, 1);
body.MethodDefinition = mainMethod;
mainMethod.Body = body;
The ILGeneratorMethodBody constructor has three parameters:
- The ILGenerator object, which ILGeneratorMethodBody uses to construct the method body’s information.
- A Boolean value that specifies whether to zero the stack on method entry. Set this parameter to
true to zero the stack.
- The maximum number of elements on the evaluation stack during method execution.
The last two lines in the example establish a link between the method body and its metadata by assigning the Main method’s
object to ILGeneratorMethodBody.MethodDefinition
, and assigning the
object to MethodDefinition.Body
Create Type and Method Definitions
You must next create definition objects for members to be called and their types. CCI Metadata includes helper objects that create an appropriate definition object and return its immutable representation. For more discussion of immutable representations, see
Mutable and Immutable Representations
Hello.exe calls one method, System.Console.WriteLine
. HelloIL creates the type definition object as follows.
INamedTypeDefinition systemConsole = UnitHelper.FindType(nameTable,
creates a NamedTypeDefinition
object for specified type and returns its immutable
has three parameters:
- The NameTable object.
- The assembly that contains the method, the .NET core assembly in this case.
- The fully-qualified type name, as an ordinary string.
HelloIL creates the WriteLine
method definition object as follows.
IMethodDefinition writeLine = TypeHelper.GetMethod(systemConsole,
creates a MethodDefinition
and returns the object’s immutable IMethodDefinition
has three parameters:
- The type definition object from the previous example.
- An IName interface for the method name, created in the usual way.
- 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 by calling ILGenerator.Emit
once for each line of code. HelloIL adds the
command, as follows:
call puts the specified instructions into the method body in the same order.
is an enumeration that contains all CLI Common Intermediate Language the operation codes.
Next: Write the Assembly to a File
Return to Beginning