C# 将方法作为参数传递
我希望能够将方法作为参数传递 例如 我如何才能做到这一点?使用C# 将方法作为参数传递,c#,.net-3.5,C#,.net 3.5,我希望能够将方法作为参数传递 例如 我如何才能做到这一点?使用Action 例如: public void CallThis(Action x) { x(); } CallThis(() => { /* code */ }); 或Func Func func1=(x)=>string.Format(“string={0}”,x); Passmea方法(“文本”,func1); public void passmea方法(字符串文本,Func func1) { Console.W
Action
例如:
public void CallThis(Action x)
{
x();
}
CallThis(() => { /* code */ });
或Func
Func func1=(x)=>string.Format(“string={0}”,x);
Passmea方法(“文本”,func1);
public void passmea方法(字符串文本,Func func1)
{
Console.WriteLine(funct1.Invoke(5));
}
建立在布鲁诺姆的基础上,因为这个例子很简单
//really dodgy code
public void PassMeAMethod(string text, Action method)
{
DoSomething(text);
method();
Foo();
}
// Elsewhere...
public static void Main(string[] args)
{
PassMeAMethod("foo", () =>
{
// Method definition here.
}
);
// Or, if you have an existing method in your class, I believe this will work
PassMeAMethod("bar", this.SomeMethodWithNoParams);
}
您需要使用委托,这是一个表示方法的特殊类。您可以定义自己的委托,也可以使用其中一个内置委托,但委托的签名必须与要传递的方法匹配 定义您自己的:
public delegate int MyDelegate(Object a);
此示例匹配一个返回整数并将对象引用作为参数的方法
在您的示例中,methodA和methodB都没有参数,并且都返回void,因此我们可以使用内置的Action委托类
以下是您的示例:
public void PassMeAMethod(string text, Action method)
{
DoSomething(text);
// call the method
method();
}
public void methodA()
{
//Do stuff
}
public void methodB()
{
//Do stuff
}
public void Test()
{
//Explicit
PassMeAMethod("calling methodA", new Action(methodA));
//Implicit
PassMeAMethod("calling methodB", methodB);
}
正如您所看到的,您可以显式或隐式地使用委托类型,以适合您的方式使用。是您需要使用的语言功能,以完成您尝试执行的操作
下面是一个使用上述代码的示例(使用代理作为快捷方式):
c#.net2.0-让我展示一下这个主题的详细答案(pass-a-method-as-a-parameter)。
在我的场景中,我正在配置一组System.Timer.Timer-s,每个都使用不同的\u Tick方法
delegate void MyAction();
// members
Timer tmr1, tmr2, tmr3;
int tmr1_interval = 4.2,
tmr2_interval = 3.5;
tmr3_interval = 1;
// ctor
public MyClass()
{
..
ConfigTimer(tmr1, tmr1_interval, this.Tmr_Tick);
ConfigTimer(tmr2, tmr2_interval, (sndr,ev) => { SecondTimer_Tick(sndr,ev); });
ConfigTimer(tmr3, tmr3_interval, new MyAction((sndr,ev) => { Tmr_Tick((sndr,ev); }));
..
}
private void ConfigTimer(Timer _tmr, int _interval, MyAction mymethod)
{
_tmr = new Timer() { Interval = _interval * 1000 };
// lambda to 'ElapsedEventHandler' Tick
_tmr.Elpased += (sndr, ev) => { mymethod(sndr, ev); };
_tmr.Start();
}
private void Tmr_Tick(object sender, ElapsedEventArgs e)
{
// cast the sender timer
((Timer)sender).Stop();
/* do your stuff here */
((Timer)sender).Start();
}
// Another tick method
private void SecondTimer_Tick(object sender, ElapsedEventArgs e) {..}
您应该能够使用代理完成此操作。您运行的是哪个版本的.NET framework?3.5,有人能告诉我如何使用上面的示例吗?谢谢这能回答你的问题吗?你能在静态空间中使用
这个?
public void PassMeAMethod(string text, Action method)
{
DoSomething(text);
// call the method
method();
}
public void methodA()
{
//Do stuff
}
public void methodB()
{
//Do stuff
}
public void Test()
{
//Explicit
PassMeAMethod("calling methodA", new Action(methodA));
//Implicit
PassMeAMethod("calling methodB", methodB);
}
//really dodgy code
public void PassMeAMethod(string text, Action method)
{
DoSomething(text);
method(); // call the method using the delegate
Foo();
}
public void methodA()
{
Console.WriteLine("Hello World!");
}
public void methodB()
{
Console.WriteLine("42!");
}
public void Test()
{
PassMeAMethod("calling methodA", methodA)
PassMeAMethod("calling methodB", methodB)
}
delegate void MyAction();
// members
Timer tmr1, tmr2, tmr3;
int tmr1_interval = 4.2,
tmr2_interval = 3.5;
tmr3_interval = 1;
// ctor
public MyClass()
{
..
ConfigTimer(tmr1, tmr1_interval, this.Tmr_Tick);
ConfigTimer(tmr2, tmr2_interval, (sndr,ev) => { SecondTimer_Tick(sndr,ev); });
ConfigTimer(tmr3, tmr3_interval, new MyAction((sndr,ev) => { Tmr_Tick((sndr,ev); }));
..
}
private void ConfigTimer(Timer _tmr, int _interval, MyAction mymethod)
{
_tmr = new Timer() { Interval = _interval * 1000 };
// lambda to 'ElapsedEventHandler' Tick
_tmr.Elpased += (sndr, ev) => { mymethod(sndr, ev); };
_tmr.Start();
}
private void Tmr_Tick(object sender, ElapsedEventArgs e)
{
// cast the sender timer
((Timer)sender).Stop();
/* do your stuff here */
((Timer)sender).Start();
}
// Another tick method
private void SecondTimer_Tick(object sender, ElapsedEventArgs e) {..}