MethodBuilder.CreateMethodBody メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > MethodBuilder.CreateMethodBody メソッドの意味・解説 

MethodBuilder.CreateMethodBody メソッド

指定した MSIL (Microsoft Intermediate Language) 命令バイト配列使用してメソッド本体作成します

名前空間: System.Reflection.Emit
アセンブリ: mscorlib (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:
void CreateMethodBody (
    array<unsigned char>^ il, 
    int count
)
public void CreateMethodBody (
    byte[] il, 
    int count
)
public function CreateMethodBody (
    il : byte[], 
    count : int
)

パラメータ

il

有効な MSIL 命令格納している配列。このパラメータnull 参照 (Visual Basic では Nothing) の場合は、メソッド本体消去されます。

count

MSIL 配列有効なバイト数。この値は、MSILnull 参照 (Visual Basic では Nothing) の場合無視されます。

例外例外
例外種類条件

ArgumentOutOfRangeException

count指定した MSIL 命令配列インデックス範囲外であり、ilnull 参照 (Visual Basic では Nothing) ではありません。

InvalidOperationException

外側の型が CreateType を使用して作成されています。

または

このメソッドは、null 参照 (Visual Basic では Nothing) ではない引数 il使用して、この MethodBuilder に対して以前呼び出されました。

または

現在のメソッドでは、IsGenericMethod プロパティtrue ですが、IsGenericMethodDefinition プロパティfalse です。

解説解説

このメソッドは、MSIL 命令格納している配列 il からメソッド本体作成します有効な MSILバイト数を count指定します

メモメモ

現在、このメソッドは完全にはサポートされていません。ユーザーは、トークン修復位置および例外ハンドラを提供できません。

使用例使用例

CreateMethodBody使用して2 つ整数加算する単純なメソッドオペコード通じて生成する例を次に示します

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,
 _
                         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 'Main

End Class 'MethodBodyDemo


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

}

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

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
MethodBuilder クラス
MethodBuilder メンバ
System.Reflection.Emit 名前空間



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「MethodBuilder.CreateMethodBody メソッド」の関連用語

MethodBuilder.CreateMethodBody メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



MethodBuilder.CreateMethodBody メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS