Creating a genericly typed Action<> at runtime

2019-07-17 07:17发布

问题:

Is it possible to create a generically typed Action at run time based on some specified types? In this particular scenario, the body of the Action will ultimately ignore the argument types, as the typed Action<> will just be a wrapper around a no-argument Action, e.g.

Action original = () => { };
...
Action<TType> wrapper = (arg) => {
    original();
}

Or, even:

Action<TTypeA, TTypeB> wrapper = (arg) => {
    original();
}

As you can see, the body of the typed Action<> ignores the arguments, and their type, it's just acting as a wrapper.

If you're curious as to why I want create this wrapper in the first place, the 'basic' version is that I am ultimately converting the Action to a Delegate for doing a Delegate.Combine(), which requires identical types. All I am trying to accomplish with the Delegate.Combine() is a basic notification that the delegate was fired.

At this point I will probably re-work my design to avoid these types of shenanigans, but I am still very curious how this might be accomplished.

The closest I could get was the following:

private static TType GetTypedDelegate<TType>(Action onComplete)
        where TType : class
    {
        MethodInfo info = typeof(TType).GetMethod("Invoke");
        ParameterInfo[] parameters = info.GetParameters();

        object result;
        if (parameters.Length == 0)
            result = onComplete;
        else if (parameters.Length == 1)
            result = GetTypedDelegate<TType>(onComplete, parameters[0].ParameterType);
        // etc

        TType onCompleteCasted = Delegate.CreateDelegate(typeof(TType), result, "Invoke") as TType;
        return onCompleteCasted;
    }

    private static Delegate GetTypedDelegate<TType>(Action onComplete, Type type)
    {
        // This line isn't useful for me right now, since I can't just create a new
        // instance of the action with a parameterless constructor ... but I thought I'd throw it in here in case it was of use
        Type actionType = typeof(Action<>).MakeGenericType(new[] { type });

        // Do some magic here with the type information
        // The following of course does not work,but you get the idea of what I am aiming for

        Action<type> wrapper = (arg1) =>
        {
            onComplete();
        };

        return wrapper as Delegate;
    }

回答1:

I think that the easiest option is to write a generic method and then invoke it dynamically (using Reflection or possibly even using C# 4 dynamic):

class Helper {
  public static Action<TType> Wrap1<TType>(Action arg) {
    return (arg) => { original(); }
  }
}

Invoking the method using Reflection and using typ1 as the generic type argument could look like this:

var meth = typeof(Helper).GetMethod("Wrap1");
var gmeth = meth.MakeGenericMethod(new[] { typ1 });
var genericAction = gmeth.Invoke(null, new object[] { action });


回答2:

If you don't want to use reflection you can setup some classes like this.

public class ActionWrapper<TTypeA>
{
    protected readonly Action _original;
    public ActionWrapper(Action original)
    {
        _original = original;
    }
    public Action<TTypeA> Wrapped { get { return WrappedAction; } }

    private void WrappedAction(TTypeA a)
    {
        _original();
    }
}

public class ActionWrapper<TTypeA,TTypeB>:ActionWrapper<TTypeA>
{
    public ActionWrapper(Action original) : base(original)
    {
    }

    public new Action<TTypeA, TTypeB> Wrapped { get { return WrappedAction; } }

    private void WrappedAction(TTypeA a,TTypeB b)
    {
        _original();
    }
}