public T ConvertByteArrayToObject <T, TK, TP>(byte[] bytes, TK obj, TP parentObj) { //获取数据包总大小 DataSize = parentObj.ExtGetPropertyValueByName <TP, short>(_innerPropertyName0) * _singleDataSize; if (!string.IsNullOrEmpty(_innerPropertyName1)) { DataSize += parentObj.ExtGetPropertyValueByName <TP, short>(_innerPropertyName1) * _singleDataSize; } //截取需要的字节数组 var allBytes = new byte[DataSize]; Array.Copy(bytes, 0, allBytes, 0, DataSize); //获取集合里面的参数类型(默认都只有一种参数类型) var type = obj.GetType(); var argument = type.GetGenericArguments()[0]; //获取单个数据包的总个数 var numPackages = DataSize / _singleDataSize; for (int i = 0; i < numPackages; i++) { //创建集合里面的对应参数类型实例 var instance = argument.ExtCreateInstance <object>(); if (instance != null) { var startLocation = i * _singleDataSize; var bodys = new List <Tuple <PropertyInfo, IBodyDataPackageAttribute, object> >(); //读取当前类包含的所有特性集合 CommonConvert.ReadChildProperty(instance.GetType().GetProperties(), instance, ref bodys); //排序 var sl = bodys.OrderBy(s => s.Item2.SortLocation); //对每个属性赋值 foreach (var packageAttribute in sl) { var newBytes = new byte[packageAttribute.Item2.DataSize]; Array.Copy(allBytes, startLocation, newBytes, 0, newBytes.Length); startLocation += packageAttribute.Item2.DataSize; packageAttribute.Item1.SetValue(instance, packageAttribute.Item2.ConvertByteArrayToObject <object, PropertyInfo>(newBytes, packageAttribute.Item1), null); //instance.ExtSetPropertyValue(packageAttribute.Item2.ConvertByteArrayToObject<object, PropertyInfo>(newBytes, packageAttribute.Item1), packageAttribute.Item1); } //像集合中添加一条数据 type.InvokeMember("Add", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, obj, new[] { instance }); } } return((T)(object)obj); }