This documentation is archived and is not being maintained.

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 Sub CreateMethodBody ( _
	il As Byte(), _
	count As Integer _
Dim instance As MethodBuilder
Dim il As Byte()
Dim count As Integer

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



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


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


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).


The containing type was previously created using CreateType.


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


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.


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.

Imports System
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit


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

   Public Shared Function BuildDynType() As Type
      Dim addType As Type = Nothing
      Dim currentDom As AppDomain = Thread.GetDomain()
      Dim myAsmName As New AssemblyName()
      myAsmName.Name = "MyDynamicAssembly"
      Dim myAsmBldr As AssemblyBuilder = currentDom.DefineDynamicAssembly(myAsmName, _
      ' The dynamic assembly space has been created.  Next, create a module
      ' within it.  The type Point will be reflected into this module.
      Dim myModuleBldr As ModuleBuilder = myAsmBldr.DefineDynamicModule("MyModule")
      Dim myTypeBldr As TypeBuilder = myModuleBldr.DefineType("Adder")
      Dim myMthdBldr As MethodBuilder = myTypeBldr.DefineMethod("DoAdd", _
					MethodAttributes.Public Or MethodAttributes.Static, _
				        GetType(Integer), _
					New Type() {GetType(Integer), GetType(Integer)})

      ' Build the array of Bytes holding the MSIL instructions.

      Dim ILcodes() As Byte = {&H2, &H3, &H58, &H2A}

      ' 02h is the opcode for ldarg.0 
      ' 03h is the opcode for ldarg.1 
      ' 58h is the opcode for add     
      ' 2Ah is the opcode for ret     
      myMthdBldr.CreateMethodBody(ILcodes, ILcodes.Length)
      addType = myTypeBldr.CreateType()
      Return addType

   End Function 'BuildDynType
   Public Shared Sub Main()
      Dim myType As Type = BuildDynType()
      Console.Write("Enter the first integer to add: ")
      Dim aVal As Integer = Convert.ToInt32(Console.ReadLine())
      Console.Write("Enter the second integer to add: ")
      Dim bVal As Integer = Convert.ToInt32(Console.ReadLine())
      Dim adderInst As Object = Activator.CreateInstance(myType, New Object() {})
      Console.WriteLine("The value of adding {0} to {1} is: {2}.", _
			 aVal, bVal, _
			 myType.InvokeMember("DoAdd", _
					      BindingFlags.InvokeMethod, _
					      Nothing, _
					      adderInst, _
					      New Object() {aVal, bVal}))

   End Sub 'Main

End Class 'MethodBodyDemo

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0