C# 回調函數


方式一:Action 無返回值泛型

參考:https://www.cnblogs.com/kybs0/p/10766329.html

方式二:Func 有返回值泛型

參考:https://www.cnblogs.com/kybs0/p/10766329.html

方式三:實例泛型

 

方案一、適用HTTP請求回調

//定義委托
public delegate void FuncEventHandler(int error, string info, string data);
//定義委托方法
public void OnRecord(int error, string info, string data, FuncEventHandler callback)
{
    callback(error, info, data);
}
//委托實現
public static void Callback(int error, string info, string data)
{
    Console.Write(data);
}
//委托調用
public MainWindow()
{
    InitializeComponent();
    //調用一
    OnRecord(-1, "信息內容", "數據內容", Callback);
    //調用二
    OnRecord((a, b, c) => { });
}

  

方案二、適用SOCKET請求回調

//存儲委托
public Dictionary<string, FuncEventHandler> dic = new Dictionary<string, FuncEventHandler>();
//定義委托
public delegate void FuncEventHandler(int error, string info, string data);
//委托實現
public static void Callback(int error, string info, string data)
{
    Console.Write(data);
}
public MainWindow()
{
    InitializeComponent();
    //存儲指定方法
    dic.Add("a", Callback);
    //存儲匿名方法
    dic.Add("b", (a, b, c) => { });

    //委托調用
    dic["a"](-1, "信息內容", "數據內容");
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM