tags:

views:

332

answers:

1

I have written a helper class which uses the Action - delegate as method parameter.
Like this:
public void SomeMethod(Action<T> methodToExecute, T argument);

According to the MSDN you can declare max. 4 arguments on an action delegate: Action<T1,T2,T3,T4>.

Now I'd like to call a method which needs 5! arguments. How could I do this?
The best solution would be something where I could use a dynamic number of method arguments.

Thanks

+12  A: 

Declare the action delegate you need, there's nothing magic about it:

public delegate void Action<T1, T2, T3, T4, T5>(T1 p1, T2 p2, T3 p3, T4 p4, T5 p5);
Lasse V. Karlsen
When .NET4 arrives the built-in `Action` and `Func` delegates will allow up to 16 parameters: http://msdn.microsoft.com/en-us/library/dd402872(VS.100).aspx
LukeH
I think that a delegate or method with 16 parameters is a serious code smell...
Thomas Levesque
I'm sorry but the following method signature does not compile:public static void Throws<TException, TFirst, TSecond, TThird, TFourth, TFifth>(Action<TFirst, TSecond, TThird, TFourth,TFifth> methodToExecute, TFirst first, TSecond second, TThird third, TFourth forth, TFifth fifth) where TException : System.Exception { /// some code }It tells me: "The non-generic type'System.Action' cannot be used with type arguments."
TomTom
And did you declare the Action delegate with five generic types?
Lasse V. Karlsen
The declaration was wrong. Typing is hard ;-) Now it is accepted and I can use it.Anyway, next time I need e.g. 6 params I have to declare another new delegate an so on. How could we make this more felxible?
TomTom
Define all those delegates in your class library so that they're there the next time you need them. Unfortunately there is no way for generics in .NET or C# to handle an unlimited number of type arguments.
Lasse V. Karlsen
Ok. Unfortunately ... But I can live with that.I have to look that my "customer" improves his code. They like to declare methods with thousands of parameters instead of using suitable classes.
TomTom