public static object DeSerialize(Type DestType, byte[] bytes) { try { if (typeof(IEntityBufObject).IsAssignableFrom(DestType)) { var desttypename = DestType.Name; IEntityBufObject ientitybufobj = null; if (!IEntityBufObjectDic.TryGetValue(desttypename, out ientitybufobj)) { lock (IEntityBufObjectDic) { ientitybufobj = (IEntityBufObject)System.Activator.CreateInstance(DestType); IEntityBufObjectDic.Add(desttypename, ientitybufobj); } } return(ientitybufobj.DeSerialize(bytes)); } var ms = new MemoryStream(bytes); BinaryReader reader = new BinaryReader(ms); MemoryStreamReader2 rd = new MemoryStreamReader2(reader); var obj = DeSerialize(DestType, rd); return(obj); } catch (Exception ex) { var e = new Exception(string.Format("无法把二进制反序列化成{0}对象", DestType.FullName), ex); e.Data.Add("bytes", bytes == null ? "" : Convert.ToBase64String(bytes)); throw e; } }
private static object DeserializeSimple(EntityBufType buftype, bool isArray, MemoryStreamReader2 msReader) { if (buftype.EntityType == EntityType.COMPLEX) { throw new Exception("无法反序列化复杂类型"); } if (buftype.EntityType == EntityType.UNKNOWN) { throw new Exception("无法反序列化未知类型"); } switch (buftype.EntityType) { case EntityType.BYTE: if (isArray) { return(msReader.ReadByteArray()); } else { return(msReader.ReadByte()); } case EntityType.STRING: if (isArray) { return(msReader.ReadStringArray()); } else { return(msReader.ReadString()); } case EntityType.CHAR: if (isArray) { return(msReader.ReadCharArray()); } else { return(msReader.ReadChar()); } case EntityType.SHORT: case EntityType.INT16: if (isArray) { return(msReader.ReadInt16Array()); } else { return(msReader.ReadInt16()); } case EntityType.USHORT: if (isArray) { return(msReader.ReadUInt16Array()); } else { return(msReader.ReadUInt16()); } case EntityType.INT32: if (isArray) { return(msReader.ReadInt32Array()); } else { return(msReader.ReadInt32()); } case EntityType.INT64: if (isArray) { return(msReader.ReadInt64Array()); } else { return(msReader.ReadInt64()); } case EntityType.DOUBLE: if (isArray) { return(msReader.ReadDoubleArray()); } else { return(msReader.ReadDouble()); } case EntityType.FLOAT: if (isArray) { return(msReader.ReadFloatArray()); } else { return(msReader.ReadFloat()); } case EntityType.DECIMAL: if (isArray) { return(msReader.ReadDeciamlArray()); } else { return(msReader.ReadDecimal()); } case EntityType.DATETIME: if (isArray) { return(msReader.ReadDateTimeArray()); } else { return(msReader.ReadDateTime()); } case EntityType.BOOL: if (isArray) { return(msReader.ReadBoolArray()); } else { return(msReader.ReadBool()); } case EntityType.ENUM: if (isArray) { string[] strarray = msReader.ReadStringArray(); Array arr = (Array)Activator.CreateInstance(buftype.ValueType, strarray.Length); for (int i = 0; i < strarray.Length; i++) { arr.SetValue(Enum.Parse(buftype.ClassType, strarray[i]), i); } return(arr); } else { return(Enum.Parse(buftype.ClassType, msReader.ReadString())); } case EntityType.DICTIONARY: if (isArray) { int arrlen = msReader.ReadInt32(); if (arrlen == -1) { return(null); } var dicarr = (Array)Activator.CreateInstance(buftype.ValueType, arrlen); for (int i = 0; i < arrlen; i++) { dicarr.SetValue(DeSerialize(buftype.ClassType, msReader), i); } return(dicarr); } else { int dicLen = msReader.ReadInt32(); if (dicLen == -1) { return(null); } IDictionary idic = (IDictionary)Activator.CreateInstance(buftype.ValueType); //var keyvaluetype = GetDirctionaryKeyValueType(buftype.ValueType); for (int i = 0; i < dicLen; i++) { idic.Add(DeSerialize(buftype.GenerTypes[0], msReader), DeSerialize(buftype.GenerTypes[1], msReader)); } return(idic); } case EntityType.LIST: if (isArray) { var listarrlen = msReader.ReadInt32(); if (listarrlen == -1) { return(null); } var listArray = (Array)Activator.CreateInstance(buftype.ValueType, listarrlen); for (int i = 0; i < listarrlen; i++) { listArray.SetValue(DeSerialize(buftype.ClassType, msReader), i); } return(listArray); } else { var listlen = msReader.ReadInt32(); if (listlen == -1) { return(null); } var list = (IList)Activator.CreateInstance(buftype.ValueType); var listvaluetype = GetListValueType(buftype.ValueType); for (int i = 0; i < listlen; i++) { list.Add(DeSerialize(listvaluetype, msReader)); } return(list); } case EntityType.ARRAY: if (isArray) { var listarrlen = msReader.ReadInt32(); if (listarrlen == -1) { return(null); } var listArray = (Array)Activator.CreateInstance(buftype.ValueType, listarrlen); for (int i = 0; i < listarrlen; i++) { listArray.SetValue(DeSerialize(buftype.ClassType, msReader), i); } return(listArray); } else { var arrlen = msReader.ReadInt32(); if (arrlen == -1) { return(null); } var arr = (Array)Activator.CreateInstance(buftype.ValueType, arrlen); var listvaluetype = GetListValueType(buftype.ValueType); for (int i = 0; i < arrlen; i++) { arr.SetValue(DeSerialize(listvaluetype, msReader), i); } return(arr); } default: throw new Exception("反序列化错误"); } }
private static object DeSerialize(Type DestType, MemoryStreamReader2 msReader) { var firstByte = (EntityBufTypeFlag)msReader.ReadByte(); if ((firstByte & EntityBufTypeFlag.VlaueNull) == EntityBufTypeFlag.VlaueNull) { return(null); } //EntityBufType destTypeBufType = MapBufType(DestType, out isArray); Tuple <EntityBufType, bool> touple = GetTypeBufType(DestType); if (touple.Item1.EntityType != EntityBuf.EntityType.COMPLEX) { return(DeserializeSimple(touple.Item1, touple.Item2, msReader)); } bool isArray; object ret = System.Activator.CreateInstance(DestType); //PropertyInfo[] props = DestType.GetProperties(); var buftypelist = GetTypeEntityBufType(DestType); Tuple <EntityBufType, bool> buftype = null; for (int k = 0; k < buftypelist.Length; k++) { buftype = buftypelist[k]; //EntityBufType buftype = MapBufType(prop.PropertyType, out isArray); isArray = buftype.Item2; if (buftype.Item1.EntityType == EntityType.COMPLEX) { if (isArray) { int len = msReader.ReadInt32(); if (len == -1) { //ret.SetValue(buftype.Item1.Property, null); //ret.SetValueDrect(buftype.Item1.Property, null); continue; } else { object[] objs = (object[])System.Activator.CreateInstance(buftype.Item1.Property.PropertyInfo.PropertyType, len); for (int i = 0; i < len; i++) { //读下标志 EntityBufTypeFlag flag = (EntityBufTypeFlag)msReader.ReadByte(); if ((flag & EntityBufTypeFlag.VlaueNull) == EntityBufTypeFlag.VlaueNull) { objs[i] = null; } else { //string typefullname = string.Format("{0}, {1}", buftype.Item1.Property.PropertyType.FullName.Substring(0, buftype.Item1.Property.PropertyType.FullName.LastIndexOf('[')), //buftype.Item1.Property.PropertyType.Assembly.FullName); //objs[i] = DeSerialize(Type.GetType(typefullname, false, true), msReader); objs[i] = DeSerialize(buftype.Item1.ClassType, msReader); } } if (!object.Equals(objs, buftype.Item1.DefaultValue)) { ret.SetValue(buftype.Item1.Property, objs); } } } else { byte byte1 = 0; byte byte2 = 0; while (true) { byte1 = msReader.ReadByte(); if (byte1 == complexchar) { byte2 = msReader.ReadByte(); if (byte2 == complexchar) { break; } } } //读下标志 EntityBufTypeFlag flag = (EntityBufTypeFlag)msReader.ReadByte(); if ((flag & EntityBufTypeFlag.VlaueNull) == EntityBufTypeFlag.VlaueNull) { //ret.SetValue(buftype.Item1.Property, null); continue; } else { object val = DeSerialize(buftype.Item1.Property.PropertyInfo.PropertyType, msReader); if (!object.Equals(val, buftype.Item1.DefaultValue)) { ret.SetValue(buftype.Item1.Property, val); } } } } else { object val = DeserializeSimple(buftype.Item1, isArray, msReader); if (!object.Equals(val, buftype.Item1.DefaultValue)) { ret.SetValue(buftype.Item1.Property, val); } } } return(ret); }