DynamicMethod.GetILGenerator Method (Int32)


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)

public ILGenerator GetILGenerator(
	int 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.

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.


There are restrictions on unverifiable code in dynamic methods, even in some full-trust scenarios. See the "Verification" section in Remarks for DynamicMethod.

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 Console.WriteLine to be used in Hello.
Type[] writeStringArgs = {typeof(string)};
// Get the overload of Console.WriteLine that has one
// String parameter.
MethodInfo writeString = typeof(Console).GetMethod("WriteLine", 

// 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);
// Load the first argument, which is a string, onto the stack.
// Call the overload of Console.WriteLine that prints a string.
il.EmitCall(OpCodes.Call, writeString, null);
// The Hello method returns the value of the second argument;
// to do this, load the onto the stack and return.

.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.1
Return to top