Func for 5 arguments

為{幸葍}努か 提交于 2019-12-13 19:17:40

问题


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?


回答1:


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)




回答2:


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:


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.



来源:https://stackoverflow.com/questions/3582866/func-for-5-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!