This is actually a better approach (bearing in mind that Johannes indicates that you could also use a higher order function), since it is most convenient for inputting types (delegates will be automatically matched with the correct number and types of arguments) and does not require any annoying DynamicInvoke calls.
However, the latter definition of the method is problematic. An Action , by its very nature, does not accept any parameters, so it will not play well with the params object[] argument. If you want a final overload that takes a variable number of arguments, I would go with DynamicInvoke in the end, just for this method call:
public void Execute(Delegate action, params object[] parameters) { DoStuff(); action.DynamicInvoke(parameters); DoMoreStuff(); }
But to expand on what Johannes was talking about, I think he basically got something like this:
public Action<T1, T2, T3> CreateAction<T1, T2, T3>(Action action) { return (T1 x, T2 y, T3 z) => action(); } public Action<T1, T2, T3> CreateAction<T1, T2, T3>(Action<T1> action, T1 arg) { return (T1 x, T2 y, T3 z) => action(arg); }
And so on - in other words, what you have already done, but in a general context, so that the code is reused elsewhere.
Dan tao
source share