MethodBuilder::CreateMethodBody Method (array<Byte>^, Int32)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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)

void CreateMethodBody(
	array<unsigned char>^ il,
	int count


Type: array<System::Byte>^

An array containing valid MSIL instructions. If this parameter is null, the method's body is cleared.

Type: System::Int32

The number of valid bytes in the MSIL array. This value is ignored if MSIL is null.

Exception Condition

The count is not within the range of indexes of the supplied MSIL instruction array and il is not null.


The containing type was previously created using CreateType.


This method was called previously on this MethodBuilder with an il argument that was not null.


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.

using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
class MethodBodyDemo

   // This class will demonstrate how to create a method body using
   // the MethodBuilder::CreateMethodBody(Byte[], int) method.
   static Type^ BuildDynType()
      Type^ addType = nullptr;
      AppDomain^ currentDom = Thread::GetDomain();
      AssemblyName^ myAsmName = gcnew 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" );
      array<Type^>^temp0 = {int::typeid,int::typeid};
      MethodBuilder^ myMthdBldr = myTypeBldr->DefineMethod( "DoAdd", static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::Static), int::typeid, temp0 );

      // Build the array of Bytes holding the MSIL instructions.

      /* 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     */
      array<Byte>^temp1 = {0x02,0x03,0x58,0x2A};
      array<Byte>^ILcodes = temp1;
      myMthdBldr->CreateMethodBody( ILcodes, ILcodes->Length );
      addType = myTypeBldr->CreateType();
      return addType;


int main()
   Type^ myType = MethodBodyDemo::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, gcnew array<Object^>(0) );
   array<Object^>^temp1 = {aVal,bVal};
   Console::WriteLine( "The value of adding {0} to {1} is: {2}.", aVal, bVal, myType->InvokeMember( "DoAdd", BindingFlags::InvokeMethod, nullptr, adderInst, temp1 ) );

.NET Framework
Available since 1.1
Available since 2.0
Return to top