C#-委托
一、简单委托声明
// 第一种-委托事件
public delegate void ChangeTextBox(string txt); // 声明一个委托-在class(类)外
public static event ChangeTextBox ChangeTextBoxValue; // 定义一个委托事件-class(类)中
ChangeTextBoxValue(this.txt1.Text); // 使用委托事件-方法里
// 第一种-委托对象
public delegate void UpdateTxt(string received_data); // 声明一个委托-在class(类)外
public UpdateTxt updateTxt; // 定义一个委托变量-class(类)中
updateTxt = new UpdateTxt(UpdateTxtMethod); // 实例化委托-方法里
二、泛型委托
1、Action<T>:无返回值
// class2
Action<UaResultLogType, string> loggingCallBack += Logging; // 回调委托
class1.Test(loggingCallBack);
private void Logging(ResultLogType type, string message)
{
// 处理。。。
}
// class1
Test(Action<UaResultLogType, string> loggingCallBack1){ // Test方法
}
2、Func<T,TResult>:有返回值
后补
三、异步使用委托-AsyncCallback
Action<BillGenRecordReqModel> PushSaveBillGenRecord = ( req) =>
{
PushSaveBillGenRecord_Post(req); //自定义的方法
};
AsyncCallback PushSaveBillGenRecordFun = (result) =>
{
((Action<BillGenRecordReqModel>)result.AsyncState).EndInvoke(result);
};
四、线程中的委托(有参与无参):
Thread类接收ThreadStart委托或ParameterizedThreadStart委托的构造函数的模板
五、WinForm与WPF中UI线程的委托(Dispatcher.Invoke):
本文来自博客园,作者:꧁执笔小白꧂,转载请注明原文链接:https://www.cnblogs.com/qq2806933146xiaobai/p/14145099.html