开发者

Passing a function with arguments to a delegate expecting none

开发者 https://www.devze.com 2023-03-10 21:47 出处:网络
I know in Python you can use functools.partial to pass a function object with some or all of the parameters already defined.

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));
0

精彩评论

暂无评论...
验证码 换一张
取 消