當前位置: 首頁>>代碼示例>>C#>>正文


C# MethodBuilder.CreateMethodBody方法代碼示例

本文整理匯總了C#中System.Reflection.Emit.MethodBuilder.CreateMethodBody方法的典型用法代碼示例。如果您正苦於以下問題:C# MethodBuilder.CreateMethodBody方法的具體用法?C# MethodBuilder.CreateMethodBody怎麽用?C# MethodBuilder.CreateMethodBody使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Reflection.Emit.MethodBuilder的用法示例。


在下文中一共展示了MethodBuilder.CreateMethodBody方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: BuildDynType

//引入命名空間
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})); 
   }
}
開發者ID:.NET開發者,項目名稱:System.Reflection.Emit,代碼行數:73,代碼來源:MethodBuilder.CreateMethodBody


注:本文中的System.Reflection.Emit.MethodBuilder.CreateMethodBody方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。