Export (0) Print
Expand All

MethodBuilder.CreateMethodBody Method

Creates the body of the method using a supplied byte array of Microsoft intermediate language (MSIL) instructions.

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

public void CreateMethodBody (
	byte[] il,
	int count
)
public void CreateMethodBody (
	byte[] il, 
	int count
)
public function CreateMethodBody (
	il : byte[], 
	count : int
)
Not applicable.

Parameters

il

An array containing valid MSIL instructions. If this parameter is a null reference (Nothing in Visual Basic), the method's body is cleared.

count

The number of valid bytes in the MSIL array. This value is ignored if MSIL is a null reference (Nothing in Visual Basic).

Exception typeCondition

ArgumentOutOfRangeException

The count is not within the range of indexes of the supplied MSIL instruction array and il is not a null reference (Nothing in Visual Basic).

InvalidOperationException

The containing type was previously created using CreateType.

-or-

This method was called previously on this MethodBuilder with an il argument that was not a null reference (Nothing in Visual Basic).

-or-

For the current method, the IsGenericMethod property is true, but the IsGenericMethodDefinition property is false.

This method creates the method's body from il, an array containing MSIL instructions as opcodes. The number of bytes of valid MSIL is given by count.

NoteNote:

This is currently not fully supported. The user cannot supply the location of token fix ups and exception handlers.

In the example provided below, a simple method that adds two integers is generated via opcode using CreateMethodBody.


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

class MethodBodyDemo {
// This class will demonstrate how to create a method body using 
// the MethodBuilder.CreateMethodBody(byte[], int) method.

   public static Type BuildDynType() {
    
  	Type addType = null;

    	AppDomain currentDom = Thread.GetDomain();

        AssemblyName myAsmName = new AssemblyName();
	myAsmName.Name = "MyDynamicAssembly";

        AssemblyBuilder myAsmBldr = currentDom.DefineDynamicAssembly(
					       myAsmName,
					       AssemblyBuilderAccess.RunAndSave);

        // The dynamic assembly space has been created.  Next, create a module
        // within it.  The type Point will be reflected into this module.

	ModuleBuilder myModuleBldr = myAsmBldr.DefineDynamicModule("MyModule");
      
	TypeBuilder myTypeBldr =  myModuleBldr.DefineType("Adder");

        MethodBuilder myMthdBldr = myTypeBldr.DefineMethod("DoAdd",
							    MethodAttributes.Public |
							    MethodAttributes.Static,
							    typeof(int),
							    new Type[] 
							    {typeof(int), typeof(int)});
        // Build the array of Bytes holding the MSIL instructions.

        byte[] ILcodes = new byte[] {
          0x02,   /* 02h is the opcode for ldarg.0 */
	  0x03,   /* 03h is the opcode for ldarg.1 */
	  0x58,   /* 58h is the opcode for add     */
	  0x2A    /* 2Ah is the opcode for ret     */
	};
	
	myMthdBldr.CreateMethodBody(ILcodes, ILcodes.Length);

        addType = myTypeBldr.CreateType();

	return addType;
   }

   public static void Main() {

	Type myType = BuildDynType(); 
        Console.WriteLine("---");
	Console.Write("Enter the first integer to add: "); 
        int aVal = Convert.ToInt32(Console.ReadLine());
     
     	Console.Write("Enter the second integer to add: ");
     	int bVal = Convert.ToInt32(Console.ReadLine());
   
     	object adderInst = Activator.CreateInstance(myType, new object[0]); 

	Console.WriteLine("The value of adding {0} to {1} is: {2}.",
			   aVal, bVal,	
     			   myType.InvokeMember("DoAdd",
			  		       BindingFlags.InvokeMethod,
			  		       null,
			  		       adderInst,
			  		       new object[] {aVal, bVal})); 
   }

}


Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft