vendredi 9 décembre 2016

Convert class method into IL and execute it at runtime

I want to convert a method into an IL codes from a class and then execute it by invoke it. The example i am following is from msdn: http://ift.tt/2hdntym.

It shows exactly what i need, my problem is to generate the ILcodes from a class method.

So basically i need to fill the following

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

from a method defined in a class for example:

public class MethodBodyDemo
{ 
    public int Add(int x, int y)
    {
        return x + y;
    }
}

I tried the following call to fill the byte array:

var ILcodes = typeof(MethodBodyDemo).GetMethod("Add").GetMethodBody().GetILAsByteArray();

The following is the example i am creating but it gives an exception: "Common Language Runtime detected an invalid program."

 public class MethodBodyDemo
{ 
    public int Add(int x, int y)
    {
        return x + y;
    }

    // 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("Add",
                                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     */
        //};

        var ILcodes = typeof(MethodBodyDemo).GetMethod("Add").GetMethodBody().GetILAsByteArray();

        myMthdBldr.CreateMethodBody(ILcodes, ILcodes.Length);

        addType = myTypeBldr.CreateType();

        return addType;
    }

    public static void TestExecMethod()
    {

        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("Add",
                                 BindingFlags.InvokeMethod,
                                 null,
                                 adderInst,
                                 new object[] { aVal, bVal }));
    }

}

Execute by calling:

MethodBodyDemo.TestExecMethod();

Any help please?





Aucun commentaire:

Enregistrer un commentaire