Export (0) Print
Expand All

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
)

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.

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.

NoteNote

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", 
    writeStringArgs);

// 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.
il.Emit(OpCodes.Ldarg_0);
// 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.
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ret);

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft