今天接手wpf版的交通管理项目,由于前人一走,剩下一堆半成熟的代码交由我负责,随之我就开始了痛苦的阅读代码的过程。
前人是一个从事了5年的高手,写的代码很高深,各种委托、事件、lambda、回调到处都是,向我这种只懂简单的委托和事件的小白,读起来可想多痛苦。最痛苦的是,很少甚至没有注释,我顿时无语了。工作还得做,毕竟还得混下去,我就慢慢看他写的代码。今天看他的某个代码中的某个方法,具体如下。
下面的这个方法是执行查询的某个方法
1 ///2 /// 执行查询 3 /// 4 /// 5 /// 6 /// 7 /// 8 /// 9 /// 10 private void QueryFromServer(DateTime startTime, DateTime endTime, ListlistGateChannel, 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 }
这个方法是上面最后一个参数(也是匿名方法里的某个方法)
1 ///2 /// 获取第一页通行记录 3 /// 4 /// 5 /// 6 /// 7 /// 8 /// 9 public void GetVehicleAccess(DateTime startTime, DateTime endTime, ListlstChannel, 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 }
看完之后是不是有点头大。经过各种查资料和分析,发现第一个方法的最后一个参数是个lambda表达式,也就是个匿名方法。方法作为参数传递,我们首先就想到是委托,然后在这匿名方法里面执行了一些操作,这个时候发现,这个还是个回调。为什么这么说呢,因为我们没有传值给这个匿名方法啊,但是我们确实用了这个值参啊。又根据回调的定义,顿时明了,被调函数传递一个方法给主调函数,主调函数传值给被调,再执行被调函数。