什么是回调,它在c#中是如何实现的?
当前回答
我才刚认识你, 这很疯狂, 这是我的number (delegate) 所以如果发生了什么(事件), 打给我吧(回电话)?
其他回答
我才刚认识你, 这很疯狂, 这是我的number (delegate) 所以如果发生了什么(事件), 打给我吧(回电话)?
定义
回调是可执行的代码 作为参数传递给其他代码。
实现
// Parent can Read
public class Parent
{
public string Read(){ /*reads here*/ };
}
// Child need Info
public class Child
{
private string information;
// declare a Delegate
delegate string GetInfo();
// use an instance of the declared Delegate
public GetInfo GetMeInformation;
public void ObtainInfo()
{
// Child will use the Parent capabilities via the Delegate
information = GetMeInformation();
}
}
使用
Parent Peter = new Parent();
Child Johny = new Child();
// Tell Johny from where to obtain info
Johny.GetMeInformation = Peter.Read;
Johny.ObtainInfo(); // here Johny 'asks' Peter to read
链接
c#的更多细节。
在计算机编程中,回调是作为参数传递给其他代码的可执行代码。 -维基百科:Callback(计算机科学)
c#有为此目的的委托。它们被大量用于事件,因为一个事件可以自动调用许多附加的委托(事件处理程序)。
回调工作步骤:
1)我们必须实现ICallbackEventHandler接口
2)注册客户端脚本:
String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");
String callbackScript = "function UseCallBack(arg, context)" + "{ " + cbReference + ";}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UseCallBack", callbackScript, true);
1)从UI调用在客户端点击调用javascript函数为EX:- builpopup(p1,p2,p3…)
Var finalfield= p1,p2,p3; UseCallBack (finalfield”、“);使用UseCallBack将客户端的数据传递给服务器端
在eventArgument中我们获取传递的数据 //执行一些服务器端操作并传递给"callbackResult"
3) GetCallbackResult() //使用此方法数据将被传递到客户端(ReceiveServerData()函数)端
callbackResult
4)获取客户端数据: ReceiveServerData(text),在文本服务器响应中,我们将得到。
回调是传递给另一个函数的函数指针。你正在调用的函数将在另一个函数完成时“回调”(执行)。
查看这个链接。
推荐文章
- 拒绝访问该路径
- Visual Studio - Resx文件默认“内部”为“公共”
- 使用linq转换列表到字典,不用担心重复
- 单元测试:日期时间。现在
- 什么是回调?
- 返回匿名类型的结果?
- 何时使用IList,何时使用List
- ConfigurationManager。AppSettings在.NET Core 2.0中可用?
- 在c#的控制台应用程序中使用'async
- 在单元测试中设置HttpContext.Current.Session
- 如何开始开发Internet Explorer扩展?
- 更新行,如果它存在,否则插入逻辑实体框架
- 在什么情况下SqlConnection会自动被征召到环境事务范围事务中?
- 用c#解析JSON
- Windows窗体中的标签的换行