Skip to main content
added 6553 characters in body
Source Link

C# - 12355395

Lets emit the code for addition using a dynamic method. Actually I hate copy and pasteemit code, so I am going with the simplest complicated way, without a million branches of nested if. I could add a million OpCodes.Nop, what is the the point in that? Lets count statements instead, which are 9 in theturns emits code to call integer addition. The function Add() below builds a dynamic method, which when run builds a dynamic method to call integer addition.

using System;
using System.Reflection.Emit;

namespace JA
{
class Program
{
    static void Main(string[] args)
    {
        int z=Add(1, 2);
        // z = 3
    }

    // Emit MSIL directlyto emit MSIL
    public static int Add(int x, int y)
    {
        Type delegate_type=typeof(Func<int, int, int>);
        DynamicMethod method = newmethod=new DynamicMethod(typeof(int).ToString()+".op_Addition",
            typeof(int),
            new Type[] { typeof(int), typeof(int) }, typeof(Program));
        ILGenerator generator=method.GetILGenerator();

        LocalBuilder method1=generator.DeclareLocal(typeof(ProgramDynamicMethod));
        ILGeneratorLocalBuilder generator=methodgenerator1=generator.GetILGeneratorDeclareLocal(typeof(ILGenerator));
        ILGeneratorLocalBuilder generator=methodadd1=generator.GetILGeneratorDeclareLocal(typeof(Func<int, int, int>));
        LocalBuilder result=generatorargs1=generator.DeclareLocal(typeof(Type[]));
        generator.Emit(OpCodes.Ldtoken, typeof(int));

        forgenerator.Emit(OpCodes.Call,
            typeof(Type).GetMethod("GetTypeFromHandle",
                System.Reflection.BindingFlags.Public|
                System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Callvirt,
            typeof(object).GetMethod("ToString",
                System.Reflection.BindingFlags.Public|
                System.Reflection.BindingFlags.Instance));
        generator.Emit(OpCodes.Ldstr, ".op_Addition");
        generator.Emit(OpCodes.Call,
            typeof(string).GetMethod("Concat",
                new Type[] { typeof(string), typeof(string) }));
        generator.Emit(OpCodes.Ldtoken, typeof(int));
 i=0; i<2; i++     generator.Emit(OpCodes.Call,
            typeof(Type).GetMethod("GetTypeFromHandle",
                System.Reflection.BindingFlags.Public|
                System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Ldc_I4, 2);
        generator.Emit(OpCodes.Newarr, typeof(Type));
        generator.Emit(OpCodes.Stloc_3);
        generator.Emit(OpCodes.Ldloc_3);
        generator.Emit(OpCodes.Ldc_I4, 0);
        generator.Emit(OpCodes.Ldtoken, typeof(int));
        generator.Emit(OpCodes.Call,
            typeof(Type).GetMethod("GetTypeFromHandle",
                System.Reflection.BindingFlags.Public|
                System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Stelem_Ref);
        generator.Emit(OpCodes.Ldloc_3);
        generator.Emit(OpCodes.Ldc_I4, 1);
        generator.Emit(OpCodes.Ldtoken, typeof(int));
        generator.Emit(OpCodes.Call,
            typeof(Type).GetMethod("GetTypeFromHandle",
                System.Reflection.BindingFlags.Public|
                System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Stelem_Ref);
        generator.Emit(OpCodes.Ldloc_3);
        generator.Emit(OpCodes.Ldtoken, typeof(Program));
        generator.Emit(OpCodes.Call,
            typeof(Type).GetMethod("GetTypeFromHandle",
                System.Reflection.BindingFlags.Public|
                System.Reflection.BindingFlags.Static));

        generator.Emit(OpCodes.Newobj,
            typeof(DynamicMethod).GetConstructor(
                new Type[] { typeof(string), typeof(Type), typeof(Type[]), typeof(Type) }));

        generator.Emit(OpCodes.Stloc_0);
        generator.Emit(OpCodes.Ldarg_SLdloc_0);
        generator.Emit(OpCodes.Callvirt, 
 i           typeof(DynamicMethod).GetMethod("GetILGenerator",
                Type.EmptyTypes));
        generator.Emit(OpCodes.Stloc_1);
        generator.Emit(OpCodes.Ldloc_1);
        generator.Emit(OpCodes.Ldtoken, typeof(int));
        generator.Emit(OpCodes.Call,
            typeof(Type).GetMethod("GetTypeFromHandle",
                System.Reflection.BindingFlags.Public|
                System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Callvirt,
            typeof(ILGenerator).GetMethod("DeclareLocal",
                new Type[] { typeof(Type) }));
        generator.Emit(OpCodes.AddPop);
        generator.Emit(OpCodes.Stloc_0Ldloc_1);
        generator.Emit(OpCodes.Ldsfld,
            typeof(OpCodes).GetField("Ldarg_0",
                System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Callvirt,
            typeof(ILGenerator).GetMethod("Emit", new Type[] { typeof(OpCode) }));
        generator.Emit(OpCodes.Ldloc_1);
        generator.Emit(OpCodes.Ldsfld,
            typeof(OpCodes).GetField("Ldarg_1",
                System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Callvirt,
            typeof(ILGenerator).GetMethod("Emit", new Type[] { typeof(OpCode) }));
        generator.Emit(OpCodes.Ldloc_1);
        generator.Emit(OpCodes.Ldsfld,
            typeof(OpCodes).GetField("Add",
                System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Callvirt,
            typeof(ILGenerator).GetMethod("Emit", new Type[] { typeof(OpCode) }));
        generator.Emit(OpCodes.Ldloc_1);
        generator.Emit(OpCodes.Ldsfld,
            typeof(OpCodes).GetField("Stloc_0",
                System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Callvirt,
            typeof(ILGenerator).GetMethod("Emit", new Type[] { typeof(OpCode) }));
        generator.Emit(OpCodes.Ldloc_1);
        generator.Emit(OpCodes.Ldsfld,
            typeof(OpCodes).GetField("Ldloc_0",
                System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Callvirt,
            typeof(ILGenerator).GetMethod("Emit", new Type[] { typeof(OpCode) }));
        generator.Emit(OpCodes.Ldloc_1);
        generator.Emit(OpCodes.Ldsfld,
            typeof(OpCodes).GetField("Ret",
                System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Callvirt,
            typeof(ILGenerator).GetMethod("Emit", new Type[] { typeof(OpCode) }));
        generator.Emit(OpCodes.Ldloc_0);
        generator.Emit(OpCodes.Ldtoken, typeof(Func<int, int, int>));
        generator.Emit(OpCodes.Call,
            typeof(Type).GetMethod("GetTypeFromHandle",
                System.Reflection.BindingFlags.Public|
                System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Callvirt,
            typeof(DynamicMethod).GetMethod("CreateDelegate",
                new Type[] { typeof(Type) }));
        generator.Emit(OpCodes.Isinst, typeof(Func<int, int, int>));
        generator.Emit(OpCodes.Stloc_2);
        generator.Emit(OpCodes.Ldloc_2);
        generator.Emit(OpCodes.Ldarg_0);
        generator.Emit(OpCodes.Ldarg_1);
        generator.Emit(OpCodes.Callvirt,
            typeof(Func<int, int, int>).GetMethod("Invoke",
                System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Instance));
        generator.Emit(OpCodes.Ret);

        Func<int, int, int> add=methodadd2=method.CreateDelegate(typeof(Func<int, int, int>)) as Func<int, int, int>;

        return addadd2(x, y);
    }
}
}

C# - 1235

Lets emit the code for addition using a dynamic method. I hate copy and paste code, so I am going with the simplest complicated way, without a million branches of nested if. I could add a million OpCodes.Nop, what is the the point in that? Lets count statements instead, which are 9 in the Add() method.

class Program
{
    static void Main(string[] args)
    {
        int z=Add(1, 2);
        // z = 3
    }

    // Emit MSIL directly
    public static int Add(int x, int y)
    {
        Type delegate_type=typeof(Func<int, int, int>);
        DynamicMethod method = new DynamicMethod(typeof(int).ToString()+".op_Addition",
            typeof(int),
            new Type[] { typeof(int), typeof(int) },
            typeof(Program));
        ILGenerator generator=method.GetILGenerator();
        ILGenerator generator=method.GetILGenerator();
        LocalBuilder result=generator.DeclareLocal(typeof(int));

        for (int i=0; i<2; i++)
        {
            generator.Emit(OpCodes.Ldarg_S, i);
        }
        generator.Emit(OpCodes.Add);
        generator.Emit(OpCodes.Stloc_0);
        generator.Emit(OpCodes.Ldloc_0);
        generator.Emit(OpCodes.Ret);

        Func<int, int, int> add=method.CreateDelegate(typeof(Func<int, int, int>)) as Func<int, int, int>;

        return add(x, y);
    }
}

C# - 5395

Lets emit code for addition using a dynamic method. Actually I emit code, which in turns emits code to call integer addition. The function Add() below builds a dynamic method, which when run builds a dynamic method to call integer addition.

using System;
using System.Reflection.Emit;

namespace JA
{
class Program
{
    static void Main(string[] args)
    {
        int z=Add(1, 2);
        // z = 3
    }

    // Emit MSIL to emit MSIL
    public static int Add(int x, int y)
    {
        Type delegate_type=typeof(Func<int, int, int>);
        DynamicMethod method=new DynamicMethod(typeof(int).ToString()+".op_Addition",
            typeof(int),
            new Type[] { typeof(int), typeof(int) }, typeof(Program));
        ILGenerator generator=method.GetILGenerator();

        LocalBuilder method1=generator.DeclareLocal(typeof(DynamicMethod));
        LocalBuilder generator1=generator.DeclareLocal(typeof(ILGenerator));
        LocalBuilder add1=generator.DeclareLocal(typeof(Func<int, int, int>));
        LocalBuilder args1=generator.DeclareLocal(typeof(Type[]));
        generator.Emit(OpCodes.Ldtoken, typeof(int));

        generator.Emit(OpCodes.Call,
            typeof(Type).GetMethod("GetTypeFromHandle",
                System.Reflection.BindingFlags.Public|
                System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Callvirt,
            typeof(object).GetMethod("ToString",
                System.Reflection.BindingFlags.Public|
                System.Reflection.BindingFlags.Instance));
        generator.Emit(OpCodes.Ldstr, ".op_Addition");
        generator.Emit(OpCodes.Call,
            typeof(string).GetMethod("Concat",
                new Type[] { typeof(string), typeof(string) }));
        generator.Emit(OpCodes.Ldtoken, typeof(int));
        generator.Emit(OpCodes.Call,
            typeof(Type).GetMethod("GetTypeFromHandle",
                System.Reflection.BindingFlags.Public|
                System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Ldc_I4, 2);
        generator.Emit(OpCodes.Newarr, typeof(Type));
        generator.Emit(OpCodes.Stloc_3);
        generator.Emit(OpCodes.Ldloc_3);
        generator.Emit(OpCodes.Ldc_I4, 0);
        generator.Emit(OpCodes.Ldtoken, typeof(int));
        generator.Emit(OpCodes.Call,
            typeof(Type).GetMethod("GetTypeFromHandle",
                System.Reflection.BindingFlags.Public|
                System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Stelem_Ref);
        generator.Emit(OpCodes.Ldloc_3);
        generator.Emit(OpCodes.Ldc_I4, 1);
        generator.Emit(OpCodes.Ldtoken, typeof(int));
        generator.Emit(OpCodes.Call,
            typeof(Type).GetMethod("GetTypeFromHandle",
                System.Reflection.BindingFlags.Public|
                System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Stelem_Ref);
        generator.Emit(OpCodes.Ldloc_3);
        generator.Emit(OpCodes.Ldtoken, typeof(Program));
        generator.Emit(OpCodes.Call,
            typeof(Type).GetMethod("GetTypeFromHandle",
                System.Reflection.BindingFlags.Public|
                System.Reflection.BindingFlags.Static));

        generator.Emit(OpCodes.Newobj,
            typeof(DynamicMethod).GetConstructor(
                new Type[] { typeof(string), typeof(Type), typeof(Type[]), typeof(Type) }));

        generator.Emit(OpCodes.Stloc_0);
        generator.Emit(OpCodes.Ldloc_0);
        generator.Emit(OpCodes.Callvirt, 
            typeof(DynamicMethod).GetMethod("GetILGenerator",
                Type.EmptyTypes));
        generator.Emit(OpCodes.Stloc_1);
        generator.Emit(OpCodes.Ldloc_1);
        generator.Emit(OpCodes.Ldtoken, typeof(int));
        generator.Emit(OpCodes.Call,
            typeof(Type).GetMethod("GetTypeFromHandle",
                System.Reflection.BindingFlags.Public|
                System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Callvirt,
            typeof(ILGenerator).GetMethod("DeclareLocal",
                new Type[] { typeof(Type) }));
        generator.Emit(OpCodes.Pop);
        generator.Emit(OpCodes.Ldloc_1);
        generator.Emit(OpCodes.Ldsfld,
            typeof(OpCodes).GetField("Ldarg_0",
                System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Callvirt,
            typeof(ILGenerator).GetMethod("Emit", new Type[] { typeof(OpCode) }));
        generator.Emit(OpCodes.Ldloc_1);
        generator.Emit(OpCodes.Ldsfld,
            typeof(OpCodes).GetField("Ldarg_1",
                System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Callvirt,
            typeof(ILGenerator).GetMethod("Emit", new Type[] { typeof(OpCode) }));
        generator.Emit(OpCodes.Ldloc_1);
        generator.Emit(OpCodes.Ldsfld,
            typeof(OpCodes).GetField("Add",
                System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Callvirt,
            typeof(ILGenerator).GetMethod("Emit", new Type[] { typeof(OpCode) }));
        generator.Emit(OpCodes.Ldloc_1);
        generator.Emit(OpCodes.Ldsfld,
            typeof(OpCodes).GetField("Stloc_0",
                System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Callvirt,
            typeof(ILGenerator).GetMethod("Emit", new Type[] { typeof(OpCode) }));
        generator.Emit(OpCodes.Ldloc_1);
        generator.Emit(OpCodes.Ldsfld,
            typeof(OpCodes).GetField("Ldloc_0",
                System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Callvirt,
            typeof(ILGenerator).GetMethod("Emit", new Type[] { typeof(OpCode) }));
        generator.Emit(OpCodes.Ldloc_1);
        generator.Emit(OpCodes.Ldsfld,
            typeof(OpCodes).GetField("Ret",
                System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Callvirt,
            typeof(ILGenerator).GetMethod("Emit", new Type[] { typeof(OpCode) }));
        generator.Emit(OpCodes.Ldloc_0);
        generator.Emit(OpCodes.Ldtoken, typeof(Func<int, int, int>));
        generator.Emit(OpCodes.Call,
            typeof(Type).GetMethod("GetTypeFromHandle",
                System.Reflection.BindingFlags.Public|
                System.Reflection.BindingFlags.Static));
        generator.Emit(OpCodes.Callvirt,
            typeof(DynamicMethod).GetMethod("CreateDelegate",
                new Type[] { typeof(Type) }));
        generator.Emit(OpCodes.Isinst, typeof(Func<int, int, int>));
        generator.Emit(OpCodes.Stloc_2);
        generator.Emit(OpCodes.Ldloc_2);
        generator.Emit(OpCodes.Ldarg_0);
        generator.Emit(OpCodes.Ldarg_1);
        generator.Emit(OpCodes.Callvirt,
            typeof(Func<int, int, int>).GetMethod("Invoke",
                System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Instance));
        generator.Emit(OpCodes.Ret);

        Func<int, int, int> add2=method.CreateDelegate(typeof(Func<int, int, int>)) as Func<int, int, int>;

        return add2(x, y);
    }
}
}
deleted 129 characters in body
Source Link

C# - 1235

Lets emit the code for addition using a dynamic method. I hate copy and paste code, so I am going with the simplest complicated way, without a million branches of nested if. I could add a million OpCodes.Nop, what is the the point in that? Lets count statements instead, which are 9 in the Add() method.

class Program
{
    static void Main(string[] args)
    {
        int z=Add(1, 2);
        // z = 3
    }

    // Emit MSIL directly
    public static int Add(int x, int y)
    {
        Type delegate_type=typeof(Func<int, int, int>);
        DynamicMethod method = new DynamicMethod(typeof(int).ToString()+".op_Addition",
            typeof(int),
            new Type[] { typeof(int), typeof(int) },
            typeof(Program));
        ILGenerator generator=method.GetILGenerator();
        ILGenerator generator=method.GetILGenerator();
        LocalBuilder result=generator.DeclareLocal(typeof(int));
        // Find the overloaded operator and create a call to it
        for (int i=0; i<2; i++)
        {
            generator.Emit(OpCodes.Ldarg_S, i);
        }
        generator.Emit(OpCodes.Add);
        generator.Emit(OpCodes.Stloc_0);
        generator.Emit(OpCodes.Ldloc_0);
        generator.Emit(OpCodes.Ret);
        // Return a delegate to call the mpiDelegateMethod
        Func<int, int, int> add=method.CreateDelegate(typeof(Func<int, int, int>)) as Func<int, int, int>;

        return add(x, y);
    }
}

C# - 1235

Lets emit the code for addition using a dynamic method. I hate copy and paste code, so I am going with the simplest complicated way, without a million branches of nested if. I could add a million OpCodes.Nop, what is the the point in that? Lets count statements instead, which are 9 in the Add() method.

class Program
{
    static void Main(string[] args)
    {
        int z=Add(1, 2);
        // z = 3
    }

    // Emit MSIL directly
    public static int Add(int x, int y)
    {
        Type delegate_type=typeof(Func<int, int, int>);
        DynamicMethod method = new DynamicMethod(typeof(int).ToString()+".op_Addition",
            typeof(int),
            new Type[] { typeof(int), typeof(int) },
            typeof(Program));
        ILGenerator generator=method.GetILGenerator();
        ILGenerator generator=method.GetILGenerator();
        LocalBuilder result=generator.DeclareLocal(typeof(int));
        // Find the overloaded operator and create a call to it
        for (int i=0; i<2; i++)
        {
            generator.Emit(OpCodes.Ldarg_S, i);
        }
        generator.Emit(OpCodes.Add);
        generator.Emit(OpCodes.Stloc_0);
        generator.Emit(OpCodes.Ldloc_0);
        generator.Emit(OpCodes.Ret);
        // Return a delegate to call the mpiDelegateMethod
        Func<int, int, int> add=method.CreateDelegate(typeof(Func<int, int, int>)) as Func<int, int, int>;

        return add(x, y);
    }
}

C# - 1235

Lets emit the code for addition using a dynamic method. I hate copy and paste code, so I am going with the simplest complicated way, without a million branches of nested if. I could add a million OpCodes.Nop, what is the the point in that? Lets count statements instead, which are 9 in the Add() method.

class Program
{
    static void Main(string[] args)
    {
        int z=Add(1, 2);
        // z = 3
    }

    // Emit MSIL directly
    public static int Add(int x, int y)
    {
        Type delegate_type=typeof(Func<int, int, int>);
        DynamicMethod method = new DynamicMethod(typeof(int).ToString()+".op_Addition",
            typeof(int),
            new Type[] { typeof(int), typeof(int) },
            typeof(Program));
        ILGenerator generator=method.GetILGenerator();
        ILGenerator generator=method.GetILGenerator();
        LocalBuilder result=generator.DeclareLocal(typeof(int));

        for (int i=0; i<2; i++)
        {
            generator.Emit(OpCodes.Ldarg_S, i);
        }
        generator.Emit(OpCodes.Add);
        generator.Emit(OpCodes.Stloc_0);
        generator.Emit(OpCodes.Ldloc_0);
        generator.Emit(OpCodes.Ret);

        Func<int, int, int> add=method.CreateDelegate(typeof(Func<int, int, int>)) as Func<int, int, int>;

        return add(x, y);
    }
}
added 68 characters in body
Source Link

C# - 10731235

Lets emit the code for addition using a dynamic method. I hate copy and paste code, so I am going with the simplest complicated way, without a million branches of nested if. I could add a million OpCodes.Nop, what is the the point in that? Lets count statements instead, which are 9 in the Add() method.

class Program
{
    static void Main(string[] args)
    {
        int z=Add(1, 2);
        // z = 3
    }

    // Emit MSIL directly
    public static int Add(int x, int y)
    {
        Type delegate_type=typeof(Func<int, int, int>);
        DynamicMethod method = new DynamicMethod(typeof(int).ToString()+".op_Addition",
            typeof(int),
            new Type[] { typeof(int), typeof(int) },
            typeof(Program));
        ILGenerator generator=method.GetILGenerator();
        ILGenerator generator=method.GetILGenerator();
        LocalBuilder result=generator.DeclareLocal(typeof(int));
        // Find the overloaded operator and create a call to it
        for (int i=0; i<2; i++)
        {
            generator.Emit(OpCodes.Ldarg_S, i);
        }
        generator.Emit(OpCodes.Add);
        generator.Emit(OpCodes.RetStloc_0);
        generator.Emit(OpCodes.Ldloc_0);
        generator.Emit(OpCodes.Ret);
        // Return a delegate to call the mpiDelegateMethod
        Func<int, int, int> add=method.CreateDelegate(typeof(Func<int, int, int>)) as Func<int, int, int>;

        return add(x, y);
    }
}

C# - 1073

Lets emit the code for addition using a dynamic method. I hate copy and paste code, so I am going with the simplest complicated way, without a million branches of nested if. Lets count statements instead, which are 9 in the Add() method.

class Program
{
    static void Main(string[] args)
    {
        int z=Add(1, 2);
        // z = 3
    }

    // Emit MSIL directly
    public static int Add(int x, int y)
    {
        Type delegate_type=typeof(Func<int, int, int>);
        DynamicMethod method = new DynamicMethod(typeof(int).ToString()+".op_Addition",
            typeof(int),
            new Type[] { typeof(int), typeof(int) },
            typeof(Program));
        ILGenerator generator=method.GetILGenerator();
        // Find the overloaded operator and create a call to it
        for (int i=0; i<2; i++)
        {
            generator.Emit(OpCodes.Ldarg_S, i);
        }
        generator.Emit(OpCodes.Add);
        generator.Emit(OpCodes.Ret);

        // Return a delegate to call the mpiDelegateMethod
        Func<int, int, int> add=method.CreateDelegate(typeof(Func<int, int, int>)) as Func<int, int, int>;

        return add(x, y);
    }
}

C# - 1235

Lets emit the code for addition using a dynamic method. I hate copy and paste code, so I am going with the simplest complicated way, without a million branches of nested if. I could add a million OpCodes.Nop, what is the the point in that? Lets count statements instead, which are 9 in the Add() method.

class Program
{
    static void Main(string[] args)
    {
        int z=Add(1, 2);
        // z = 3
    }

    // Emit MSIL directly
    public static int Add(int x, int y)
    {
        Type delegate_type=typeof(Func<int, int, int>);
        DynamicMethod method = new DynamicMethod(typeof(int).ToString()+".op_Addition",
            typeof(int),
            new Type[] { typeof(int), typeof(int) },
            typeof(Program));
        ILGenerator generator=method.GetILGenerator();
        ILGenerator generator=method.GetILGenerator();
        LocalBuilder result=generator.DeclareLocal(typeof(int));
        // Find the overloaded operator and create a call to it
        for (int i=0; i<2; i++)
        {
            generator.Emit(OpCodes.Ldarg_S, i);
        }
        generator.Emit(OpCodes.Add);
        generator.Emit(OpCodes.Stloc_0);
        generator.Emit(OpCodes.Ldloc_0);
        generator.Emit(OpCodes.Ret);
        // Return a delegate to call the mpiDelegateMethod
        Func<int, int, int> add=method.CreateDelegate(typeof(Func<int, int, int>)) as Func<int, int, int>;

        return add(x, y);
    }
}
Source Link
Loading