jeudi 13 septembre 2018

How to create lambdas and add them to actions using reflection

Suppose in C# I have class that has an arbitrary number of Actions, which can have any number of generic arguments:

public class Container
{
    public Action a;
    public Action<float> b;
    public Action<int, float> c;
    // etc...
}

And I am registering some debug lambdas on an instance of this class which just print out the name of the action's field:

public static void Main()
{
    Container container = new Container();

    container.a += () => Console.WriteLine("a was called");
    container.b += (temp1) => Console.WriteLine("b was called");
    container.c += (temp1, temp2) => Console.WriteLine("c was called");

    container.a();
    container.b(1.5f);
    container.c(1, 1.5f);
}


I would like to automate the creation of these debug lambdas using reflection, as follows:

public static void Main()
{
    Container container = new Container();

    GenerateDebug(container);

    if(container.a != null) container.a();
    if(container.b != null) container.b(1.5f);
    if(container.c != null) container.c(1, 1.5f);
}

public static void GenerateDebug(Container c)
{
    Type t = c.GetType();
    FieldInfo[] fields = t.GetFields(BindingFlags.Instance | BindingFlags.Public);
    foreach(FieldInfo field in fields)
    {
        Action callback = () => Console.WriteLine(field.Name + " was called");

        Type[] actionArgTypes = field.FieldType.GetGenericArguments();
        if(actionArgTypes.Length == 0)
        {
            Action action = field.GetValue(c) as System.Action;
            action += callback;
            field.SetValue(c, action);
        }
        else
        {
            // 1. Create an Action<T1, T2, ...> object that takes the types in 'actionArgTypes' which wraps the 'callback' action
            // 2. Add this new lambda to the current Action<T1, T2, ...> field 
        }   
    }
}

I'm able to get the desired result for Actions with no arguments - the above code does indeed print out "a was called" - but I don't know how to do it for generics.

I believe I know what I need to do, just not how:

  1. Use reflection to create an Action<T1, T2, ...> object using the types in actionArgTypes, which wraps a call to the callback action.
  2. Add this newly created object to the Generic Action specified by the field.

How would I go about doing this, or similar that achieves the desired effect of adding such a debug callback?





Aucun commentaire:

Enregistrer un commentaire