Func for 5 arguments

2019-07-30 11:21发布

I am working with System.Func but have reached a stumbling block with it.

System.Func<TReturn> // (no arg, with return value)
System.Func<T, TReturn> // (1 arg, with return value)
System.Func<T1, T2, TReturn> // (2 arg, with return value)
System.Func<T1, T2, T3, TReturn> // (3 arg, with return value)
System.Func<T1, T2, T3, T4, TReturn> // (4 arg, with return value)

The max it accepts is 4 arguments.

Is there any way of extending this to 5 arguments?

3条回答
老娘就宠你
2楼-- · 2019-07-30 11:59

Move to a higher version of .NET framework. E.g. .NET 4.0 has up to 16

http://msdn.microsoft.com/en-us/library/yxcx7skw.aspx

whereas 3.5 has just 4

http://msdn.microsoft.com/en-us/library/yxcx7skw(v=VS.90).aspx

Or, if you stuck in a lower version, just define the delegate yourself.

查看更多
贪生不怕死
3楼-- · 2019-07-30 12:01

You have a few options one is to define the delegate your self which would look like:

public delegate TResult Func<T1,T2,...,TN,TResult>(T1 arg1, T2 arg2,...,TN argN); 

you can basically define it for any number of arguments (higher than 4 since you might get a name clash otherwise)

or you can wrap your arguments into a structure of some sort so that you can use one of the Func delegates already defined for you.

In any case you should worry about the method signature if you cannot use one of the predefined Func delegates. Quite often long lists of parameters are a smell that often leads you to realize that the method is doing to much (unrelated) work.

My personal approach would thus be to figure out where the design failed and correct that rather than correct what is most often the symptom (in this case defining a Func with sufficient agruments could be fixing the symptom not saying that it is since I don't know your code)

查看更多
三岁会撩人
4楼-- · 2019-07-30 12:03

I guess, it is possible to see Action and Func at least with 25 parameters in framework 5.0 :)

There is not any reason to move to the higher framework version because i cannot refactore my code to valid, maintainable view. Really it is not so good practice to use so many parameters in the method's of function's signature. Use entity that has 5 fields or properties. You can expend this entity without changing method or function signature.

查看更多
登录 后发表回答