C# 将回调函数传递给另一个类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9931723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing a callback function to another class
提问by Geesu
I'm basically trying to pass a method to another class to be called later, but can't quite figure this out in C# (I'm still too used to Objective-C).
我基本上是在尝试将一个方法传递给另一个稍后调用的类,但是在 C# 中无法完全弄清楚这一点(我仍然太习惯于 Objective-C)。
public class Class1{
private void btn_click(object sender, EventArgs e)
{
ServerRequest sr = new ServerRequest();
sr.DoRequest("myrequest", myCallback);
}
public void myCallback(string str)
{
}
}
Then later on I want my ServerRequest class to basically fire the callback function, is this not possible? (I'm basically phoning home to a server for a login response to my software)
然后我希望我的 ServerRequest 类基本上触发回调函数,这不可能吗?(我基本上是打电话给服务器以获取对我的软件的登录响应)
I haven't been able to find a way to do this with delegates, continuously get errors. Here is the other class:
我一直无法找到一种方法来处理委托,不断出现错误。这是另一个类:
public class ServerRequest
{
public void DoRequest(string request, Delegate callback)
{
// do stuff....
callback("asdf");
}
}
Is this possible in #? In Objective-C this would be simple and I would just do something like
这在# 中可能吗?在 Objective-C 中,这很简单,我会做类似的事情
[myObject performSelector(@selector(MyFunctionName)) withObjects:nil];
采纳答案by BrokenGlass
You can pass it as Action<string>- which means it is a method with a single parameter of type stringthat doesn't return anything (void) :
您可以将其传递为Action<string>- 这意味着它是一个具有string不返回任何内容 (void)类型的单个参数的方法:
public void DoRequest(string request, Action<string> callback)
{
// do stuff....
callback("asdf");
}
回答by Wiktor Zychla
You have to first declare delegate's type because delegates are strongly typed:
您必须首先声明委托的类型,因为委托是强类型的:
public void MyCallbackDelegate( string str );
public void DoRequest(string request, MyCallbackDelegate callback)
{
// do stuff....
callback("asdf");
}
回答by Robbie
Delegateis just the base class so you can't use it like that. You could do something like this though:
Delegate只是基类,所以你不能那样使用它。你可以做这样的事情:
public void DoRequest(string request, Action<string> callback)
{
// do stuff....
callback("asdf");
}
回答by Marco
You could change your code in this way:
您可以通过以下方式更改代码:
public delegate void CallbackHandler(string str);
public class ServerRequest
{
public void DoRequest(string request, CallbackHandler callback)
{
// do stuff....
callback("asdf");
}
}
回答by Maciej
回答by Jeroen
public class Class1
{
private void btn_click(object sender, EventArgs e)
{
ServerRequest sr = new ServerRequest();
sr.Callback += new ServerRequest.CallbackEventHandler(sr_Callback);
sr.DoRequest("myrequest");
}
void sr_Callback(string something)
{
}
}
public class ServerRequest
{
public delegate void CallbackEventHandler(string something);
public event CallbackEventHandler Callback;
public void DoRequest(string request)
{
// do stuff....
if (Callback != null)
Callback("bla");
}
}
回答by Nirupam
You could use only delegate which is best for callback functions:
您只能使用最适合回调函数的委托:
public class ServerRequest
{
public delegate void CallBackFunction(string input);
public void DoRequest(string request, CallBackFunction callback)
{
// do stuff....
callback(request);
}
}
and consume this like below:
并像下面这样消费:
public class Class1
{
private void btn_click(object sender, EventArgs e)
{
ServerRequest sr = new ServerRequest();
var callback = new ServerRequest.CallBackFunction(CallbackFunc);
sr.DoRequest("myrequest",callback);
}
void CallbackFunc(string something)
{
}
}

