Search code examples
c#lambdareflectionexpression-trees

How to compile a lambda expression into a dynamically created class


I'm trying to create an extension of a class using reflection emit.

And it is kinda working.

var extendwith = new List<Type>();
extendwith.Add(typeof(object));
var t = Utils.DynamicInherit<BaseTest>("Extended",typeof(UtilsTest).GetMethod(nameof(Testing)), extendwith);

var instance = Activator.CreateInstance(t, new TestClass(), new { A=1 });


public class BaseTest
{
    public readonly TestClass testClass;

    public object IsNotNull;

    public BaseTest(TestClass testClass)
    {
        this.testClass = testClass;
    }
}

public static void Testing(BaseTest baseTest, List<object> objects)
    {
        foreach(var t in objects)
        {
            baseTest.IsNotNull = t;
        }
    }

What is happening here is that the the type (t) returned from the DynamicInherit method will now inherit from the BaseTest class and have a constructor containing 2 parameters (TestClass, Object)

The static method Testing will be called inside the constructor of the "constructed type". It is called using this IL code.

constructorBuilder.Emit(OpCodes.Ldarg_0); //The this a referance to the created base class
constructorBuilder.Emit(OpCodes.Ldloc_0); //Reference to a List<Objects> that is passed in thorugh the constructor
constructorBuilder.Emit(OpCodes.Call, constructor); //The function to be called.. in this case the "public static void Testing(BaseTest baseTest, List<object> objects)"

This is working "as expected", but I would like to change it to using a lamda expression instead of the static function..

var t = Utils.DynamicInherit<BaseTest>("Extended",(baseO, objects) => {foreach(var t in objects)
        {
            baseTest.IsNotNull = t;
        }} , 
        extendwith);

I know I have to change the line

constructorBuilder.Emit(OpCodes.Call, constructor); 

But I'm not able to figure out how.... Any suggestions???


Solution

  • Given

    Action<BaseText,List<object>> f = (BaseTest baseO, List<object> objects) => {
        foreach(var t in objects) {
            baseTest.IsNotNull = t;
        }
    };
    

    Then f.Method should be what you need I think:

    var t = Utils.DynamicInherit<BaseTest>("Extended",f.Method, extendwith);
    

    Alternatively, you can combine it all:

    var t = Utils.DynamicInherit<BaseTest>("Extended",((Action<BaseTest,List<object>>)((baseO, objects) => {foreach(var t in objects)
        {
            baseTest.IsNotNull = t;
        }})).Method,
        extendwith);
    

    Using a utility class I have makes this a little easier to read:

    public static class To {
        public static Func<TResult> Func<TResult>(Func<TResult> func) => func;
        public static Func<T, TResult> Func<T, TResult>(Func<T, TResult> func) => func;
        public static Func<T1, T2, TResult> Func<T1, T2, TResult>(Func<T1, T2, TResult> func) => func;
        public static Func<T1, T2, T3, TResult> Func<T1, T2, T3, TResult>(Func<T1, T2, T3, TResult> func) => func;
        public static Func<T1, T2, T3, T4, TResult> Func<T1, T2, T3, T4, TResult>(Func<T1, T2, T3, T4, TResult> func) => func;
        public static Func<T1, T2, T3, T4, T5, TResult> Func<T1, T2, T3, T4, T5, TResult>(Func<T1, T2, T3, T4, T5, TResult> func) => func;
        public static Func<T1, T2, T3, T4, T5, T6, TResult> Func<T1, T2, T3, T4, T5, T6, TResult>(Func<T1, T2, T3, T4, T5, T6, TResult> func) => func;
        public static Func<T1, T2, T3, T4, T5, T6, T7, TResult> Func<T1, T2, T3, T4, T5, T6, T7, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, TResult> func) => func;
        public static Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult> Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult> func) => func;
        public static Action<T> Action<T>(Action<T> act) => act;
        public static Action<T1, T2> Action<T1, T2>(Action<T1, T2> act) => act;
        public static Action<T1, T2, T3> Action<T1, T2, T3>(Action<T1, T2, T3> act) => act;
        public static Action<T1, T2, T3, T4> Action<T1, T2, T3, T4>(Action<T1, T2, T3, T4> act) => act;
        public static Action<T1, T2, T3, T4, T5> Action<T1, T2, T3, T4, T5>(Action<T1, T2, T3, T4, T5> act) => act;
        public static Action<T1, T2, T3, T4, T5, T6> Action<T1, T2, T3, T4, T5, T6>(Action<T1, T2, T3, T4, T5, T6> act) => act;
        public static Action<T1, T2, T3, T4, T5, T6, T7> Action<T1, T2, T3, T4, T5, T6, T7>(Action<T1, T2, T3, T4, T5, T6, T7> act) => act;
        public static Action<T1, T2, T3, T4, T5, T6, T7, T8> Action<T1, T2, T3, T4, T5, T6, T7, T8>(Action<T1, T2, T3, T4, T5, T6, T7, T8> act) => act;
    }
    

    Then you just have:

    var t = Utils.DynamicInherit<BaseTest>("Extended", To.Action((baseO, objects) => {
        foreach(var t in objects) {
            baseTest.IsNotNull = t;
        }}).Method,
        extendwith);