MethodBuilder.CreateMethodBody(Byte[], Int32) 메서드

정의

MSIL(Microsoft Intermediate Language) 명령의 제공된 바이트 배열을 사용하여 메서드의 본문을 만듭니다.

public:
 void CreateMethodBody(cli::array <System::Byte> ^ il, int count);
public void CreateMethodBody (byte[] il, int count);
member this.CreateMethodBody : byte[] * int -> unit
Public Sub CreateMethodBody (il As Byte(), count As Integer)

매개 변수

il
Byte[]

유효한 MSIL 명령이 들어 있는 배열입니다. 이 매개 변수가 null이면 메서드 본문이 지워집니다.

count
Int32

유효한 MSIL 배열의 바이트의 수입니다. MSIL이 null이면 이 값은 무시됩니다.

예외

count이(가) 제공된 MSIL 명령 배열의 인덱스 범위 내에 있지 않으며 il이(가) null이 아닙니다.

포함하는 형식은 이전에 CreateType()을 사용하여 만든 것입니다.

또는

이 메서드는 null이 아닌 il 인수를 사용하여 이 MethodBuilder에서 이전에 호출되었습니다.

또는

현재 메서드에 대해 IsGenericMethod 속성은 true이지만 IsGenericMethodDefinition 속성은 false입니다.

예제

아래 제공된 예제에서는 를 사용하여 CreateMethodBodyopcode를 통해 두 개의 정수 를 추가하는 간단한 메서드가 생성됩니다.

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

   // 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 ) );
}

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}));
   }
}
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, _
                         AssemblyBuilderAccess.RunAndSave)
      
      ' 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.WriteLine("---")
      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

End Class

설명

이 메서드는 MSIL 명령을 opcodes로 포함하는 배열인 에서 il메서드의 본문을 만듭니다. 유효한 MSIL의 바이트 수는 개수로 지정됩니다.

참고

현재 완전히 지원되지는 않습니다. 사용자는 토큰 수정 업 및 예외 처리기의 위치를 제공할 수 없습니다.

적용 대상