/// <summary> /// /// </summary> /// <param name="baseType"></param> /// <param name="type"></param> /// <param name="valueType"></param> /// <param name="buff"></param> /// <param name="offset"></param> /// <returns></returns> protected object DoDeserialize(Type baseType, Type type, string valueType, byte[] buff, ref int offset) { if (DepthType == EnumDepthType.Depth && string.IsNullOrEmpty(valueType)) { int count = ByteConverter.Parse <int>(buff, ref offset); if (count > -1) { valueType = Encoding.UTF8.GetString(buff, offset, count); offset += count; } if (string.IsNullOrEmpty(valueType)) { throw new DMSFrameException("深度检查Type失败!"); } return(DoDeserialize(baseType, type, valueType, buff, ref offset)); } else if (DepthType == EnumDepthType.Normal && string.IsNullOrEmpty(valueType)) { int count = ByteConverter.Parse <int>(buff, ref offset); if (count > -1) { if (count == 0) { valueType = type.FullName.ToString(); } else { valueType = Encoding.UTF8.GetString(buff, offset, count); offset += count; } } if (string.IsNullOrEmpty(valueType)) { throw new DMSFrameException("深度检查Type失败!"); } return(DoDeserialize(baseType, type, valueType, buff, ref offset)); } object obj5; Type singleType = type; if (!string.IsNullOrEmpty(valueType)) { if (DepthType == EnumDepthType.Normal) { singleType = DeserializeNullAssemblyType(singleType, valueType); if (singleType == null) { singleType = type; } } else { singleType = DeserializeAssemblyType(singleType, valueType); if (singleType == null) { singleType = type; } } } if (ByteConverter.SupportType(singleType)) { return(ByteConverterFactory.Parse(singleType, buff, ref offset)); } if (ByteNullableConverter.SupportType(singleType)) { return(ByteConverterFactory.Parse(singleType, buff, ref offset)); } if (singleType == typeof(string)) { return(DeserializeString(buff, ref offset)); } if (singleType.FullName == "System.Type" || singleType.FullName == "System.RuntimeType") { string value = DeserializeString(buff, ref offset); return(Type.GetType(value)); } if (singleType == typeof(byte[])) { return(DeserializeBytes(buff, ref offset)); } if (IsGenericTypeList(singleType)) { return(DeserializeList(baseType, buff, ref offset, singleType)); } if (IsGenericTypeDictionary(singleType)) { return(DeserializeDictionary(baseType, buff, ref offset, singleType)); } if (singleType == typeof(DataTable)) { string table = DeserializeString(buff, ref offset); return(DataTableSerializer.DeserializeDataTable(table)); } if (singleType.IsArray) { return(DeserializeArray(baseType, buff, ref offset, singleType)); } if (singleType.IsGenericType) { throw new DMSFrameException(string.Format("{0}", singleType)); } try { obj5 = this.DoDeserializeComplicatedType(baseType, singleType, buff, ref offset); } catch (Exception exception) { throw new DMSFrameException(string.Format("{0}-{1}", exception.Message, singleType), exception); } return(obj5); }
/// <summary> /// 读取buffer的字节返回相应的对象 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="buff">字节数组</param> /// <param name="offset">开始位置</param> /// <returns></returns> public static T Parse <T>(byte[] buff, ref int offset) where T : struct { return((T)ByteConverterFactory.Parse(typeof(T), buff, ref offset)); }