Passing a function with arguments to a delegate expecting none
I know in Python you can use functools.partial
to pass a function object with some or all of the parameters already defined.
Is there a way to do this in C#? I would like to do the following:
class1.MethodTakingAMethodParameter(3, "foo", class1.MethodToPa开发者_Python百科ss(param1, param2, param3));
For a delegate like delegate void Blah()
.
You can use lambdas to give some parameters a value:
Action paramlessDelegate = ()=>MyFunc(value1,value2,value3)
Or if you want to only put a value into some params:
Action<int> oneParamDelegate = (remainingParam)=>MyFunc(value1, remainingParam, value3)
In your example:
class1.MethodTakingAMethodParameter(3, "foo", ()=>class1.MethodToPass(param1, param2, param3));
精彩评论