博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
委托、lambda表达式、回调
阅读量:4609 次
发布时间:2019-06-09

本文共 3272 字,大约阅读时间需要 10 分钟。

今天接手wpf版的交通管理项目,由于前人一走,剩下一堆半成熟的代码交由我负责,随之我就开始了痛苦的阅读代码的过程。

前人是一个从事了5年的高手,写的代码很高深,各种委托、事件、lambda、回调到处都是,向我这种只懂简单的委托和事件的小白,读起来可想多痛苦。最痛苦的是,很少甚至没有注释,我顿时无语了。工作还得做,毕竟还得混下去,我就慢慢看他写的代码。今天看他的某个代码中的某个方法,具体如下。

 

下面的这个方法是执行查询的某个方法

1 ///  2         /// 执行查询 3         ///  4         ///  5         ///  6         ///  7         ///  8         ///  9         /// 10         private void QueryFromServer(DateTime startTime, DateTime endTime, List
listGateChannel, string condition, int page, int pageSize)11 {12 m_rmpServiceHelper.GetVehicleAccess(startTime, endTime, listGateChannel, condition, pageSize, (lstVehicle) =>13 {14 if (lstVehicle == null || lstVehicle.Count == 0)15 {16 MessageHelper.Show("未查询到相关记录", MessageType.Ok, 1);17 return;18 }19 _listVehicle.Clear();20 foreach (VehicleInfoDisplay item in lstVehicle)21 {22 _listVehicle.Add(item);23 _allDataItems.Add(item);24 dataPager.IsShowMoreButton = true;25 }26 dataPager.TotalCount = _allDataItems.Count;27 });28 29 }
View Code

这个方法是上面最后一个参数(也是匿名方法里的某个方法)

1 ///  2         /// 获取第一页通行记录 3         ///  4         ///  5         ///  6         ///  7         ///  8         ///  9         public void GetVehicleAccess(DateTime startTime, DateTime endTime, List
lstChannel, string condition, int pageSize, Action
> callBack)10 {11 CommunicationHelper.Instance.SendMessage((client) =>12 {13 string[] arrTypes = new string[lstChannel.Count];14 int?[] arrIds = new int?[lstChannel.Count];15 for (int i = 0; i < arrTypes.Length; i++)16 {17 arrTypes[i] = "HostChannel";18 }19 for (int i = 0; i < lstChannel.Count; i++)20 {21 arrIds[i] = lstChannel[i].InternalObjectId;22 }23 m_pageInfo = client.PagePlusVehicleAccessInfoLogEx2(startTime, endTime, arrTypes, arrIds, condition, "", 1, pageSize);24 List
tempList = new List
();25 if (m_pageInfo.recordTotalNum > 0 && m_pageInfo.vehicleAccessInfo.Length > 0)26 {27 tempList.AddRange(m_pageInfo.vehicleAccessInfo.OrderBy(o => o.accessTime));28 }29 List
displayList = new List
();30 foreach (vehicleAccessInfoLogEx1 obj in tempList)31 {32 VehicleInfoDisplay display = VehicleAccessInfoToDisplay.GetVehicleInfo(obj);33 displayList.Add(display);34 }35 if (callBack != null)36 {37 DispatcherHelper.RunSync(new Action(() =>38 {39 callBack(displayList);40 }), null);41 }42 });43 44 }
View Code

看完之后是不是有点头大。经过各种查资料和分析,发现第一个方法的最后一个参数是个lambda表达式,也就是个匿名方法。方法作为参数传递,我们首先就想到是委托,然后在这匿名方法里面执行了一些操作,这个时候发现,这个还是个回调。为什么这么说呢,因为我们没有传值给这个匿名方法啊,但是我们确实用了这个值参啊。又根据回调的定义,顿时明了,被调函数传递一个方法给主调函数,主调函数传值给被调,再执行被调函数。

 

转载于:https://www.cnblogs.com/liehuochongsheng/p/4055009.html

你可能感兴趣的文章
文本CSS
查看>>
JDK1.7新特性,语言篇
查看>>
javaScript判断手机型号
查看>>
应用程序设计:图书管理系统模板(链表+文件)
查看>>
遗传算法学习--多目标优化中的遗传算法
查看>>
Git的安装和使用教程详解
查看>>
lsof命令详解
查看>>
常用模块,异常处理
查看>>
父窗口与子窗口之间的传值
查看>>
eclipse 找不到 tomcat 的解决方案
查看>>
HDU 1890--Robotic Sort(Splay Tree)
查看>>
connection string for Excel/Access 2010
查看>>
【转】【Python】Python中的__init__.py与模块导入(from import 找不到模块的问题)
查看>>
学习wavenet_vocoder之环境配置
查看>>
常用Maven命令
查看>>
Docker启动mysql的坑2
查看>>
JS中的对象数组
查看>>
j2ee爬坑行之二 servlet
查看>>
JAVA基础入门(JDK、eclipse下载安装)
查看>>
最基础的applet运用--在applet上画线
查看>>