/// <summary> /// 尝试直接反序列化基础值类型(包括其数组) /// </summary> /// <param name="type">类型</param> /// <param name="value">返回的对象</param> /// <param name="theByteReader">读取reader</param> /// <returns>能否执行</returns> public static bool TryGetValue(Type type, out object value, ZipBytesReader theByteReader) { value = null; if (type.IsEnum) { type = type.GetFields()[0].FieldType; } if (type == Type_Bool) { value = theByteReader.ReadBoolean(); return(true); } if (type == Type_Byte) { value = theByteReader.ReadByte(); return(true); } if (type == Type_SByte) { value = theByteReader.ReadSByte(); return(true); } if (type == Type_UInt16) { value = theByteReader.ReadUInt16(); return(true); } if (type == Type_Int16) { value = theByteReader.ReadInt16(); return(true); } if (type == Type_Char) { value = theByteReader.ReadChar(); return(true); } if (type == Type_UInt32) { value = theByteReader.ReadUInt32(); return(true); } if (type == Type_Int32) { value = theByteReader.ReadInt32(); return(true); } if (type == Type_UInt64) { value = theByteReader.ReadUInt64(); return(true); } if (type == Type_Int64) { value = theByteReader.ReadInt64(); return(true); } if (type == Type_Single) { value = theByteReader.ReadSingle(); return(true); } if (type == Type_Double) { value = theByteReader.ReadDouble(); return(true); } if (type == Type_Decimal) { value = theByteReader.ReadDecimal(); return(true); } if (type == Type_Guid) { value = theByteReader.ReadGuid(); return(true); } if (type == Type_DateTime) { value = theByteReader.ReadDateTime(); return(true); } if (type == Type_TimeSpan) { value = theByteReader.ReadTimeSpan(); return(true); } if (type == Type_String) { value = theByteReader.ReadString(); return(true); } bool isEnumArray = false; Type elemtT = null; FieldInfo elemtValueField = null; if (type.IsArray) { elemtT = type.GetElementType(); if (elemtT.IsEnum) { isEnumArray = true; elemtValueField = elemtT.GetFields()[0]; type = elemtValueField.FieldType.MakeArrayType(); } } if (type == Type_Bools) { value = theByteReader.ReadBooleans(); goto GoTrueEnd; } if (type == Type_Bytes) { value = theByteReader.ReadBytes(); goto GoTrueEnd; } if (type == Type_SBytes) { value = theByteReader.ReadSBytes(); goto GoTrueEnd; } if (type == Type_UInt16s) { value = theByteReader.ReadUInt16s(); goto GoTrueEnd; } if (type == Type_Int16s) { value = theByteReader.ReadInt16s(); goto GoTrueEnd; } if (type == Type_Chars) { value = theByteReader.ReadChars(); goto GoTrueEnd; } if (type == Type_UInt32s) { value = theByteReader.ReadUInt32s(); goto GoTrueEnd; } if (type == Type_Int32s) { value = theByteReader.ReadInt32s(); goto GoTrueEnd; } if (type == Type_UInt64s) { value = theByteReader.ReadUInt64s(); goto GoTrueEnd; } if (type == Type_Int64s) { value = theByteReader.ReadInt64s(); goto GoTrueEnd; } if (type == Type_Singles) { value = theByteReader.ReadSingles(); goto GoTrueEnd; } if (type == Type_Doubles) { value = theByteReader.ReadDoubles(); goto GoTrueEnd; } if (type == Type_Decimals) { value = theByteReader.ReadDecimals(); goto GoTrueEnd; } if (type == Type_Guids) { value = theByteReader.ReadGuids(); goto GoTrueEnd; } if (type == Type_DateTimes) { value = theByteReader.ReadDateTimes(); goto GoTrueEnd; } if (type == Type_TimeSpans) { value = theByteReader.ReadTimeSpans(); goto GoTrueEnd; } return(false); GoTrueEnd: if (isEnumArray && value != null) { Array oArray = value as Array; int arrayLength = oArray.Length; Array newArray = Array.CreateInstance(elemtT, arrayLength); for (int i = 0; i < arrayLength; i++) { newArray.SetValue(Enum.ToObject(elemtT, oArray.GetValue(i)), i); } value = newArray; } return(true); }
/// <summary> /// 反序列化Byte数组到一个对象,无法反序列化,将抛出ArgumentException异常 /// </summary> /// <param name="objectType">对象类型</param> /// <param name="bytesValue">二进制数据</param> /// <param name="startIndex">开始执行的位置</param> /// <param name="maxReadSize">指定支持最大反序列化的数据大小</param> /// <param name="readLength">实际反序列化时读取的元素个数</param> /// <returns>对象</returns> public static object DeserializeFrom(Type objectType, byte[] bytesValue, int startIndex, int maxReadSize, out int readLength) { try { ZipBytesReader brData = new ZipBytesReader(startIndex, maxReadSize, bytesValue); int point = startIndex; object fatherObject = null; object innerObject = null; int readEnd = 0; byte isNull = 0; int totalLength = 0; int nextSeekTo = 0; ushort metablockLen = 0; //新的字段信息,一般只保存类型和父类 SerializeInfo innerSerializeInfo; Stack <SerializeInfo> bufStack = new Stack <SerializeInfo>(); bool fullLengthSchema = false; if (brData.ReadByte() != Byte_Zero) { fullLengthSchema = true; metablockLen = brData.TryReadMetaBlockLength(); totalLength = brData.Position + metablockLen; } bufStack.Push(new SerializeInfo(objectType, totalLength)); while (bufStack.Count > 0) { //当前的字段信息 SerializeInfo nowSerializeInfo = bufStack.Peek(); readEnd = nowSerializeInfo.ByteStartIndexOrEndPoint; if (nowSerializeInfo.TheObject == null) //未检查过 { //专门处理Nullable类型 if (nowSerializeInfo.TheType.IsGenericType) { if (nowSerializeInfo.TheType.GetGenericTypeDefinition() == SerializeInfo.Type_Nullable) { isNull = brData.ReadByte(); if (isNull == Byte_Zero) //为null { nowSerializeInfo.FatherSerializeInfo.Index++; bufStack.Pop(); if (fullLengthSchema) { if (readEnd >= totalLength) { break; } brData.SeekNextTo(readEnd); } continue; } else { nowSerializeInfo.TheType = nowSerializeInfo.TheType.GetGenericArguments()[0]; } } } //基础类 if (BytesHelper.TryGetValue(nowSerializeInfo.TheType, out innerObject, brData)) { if (nowSerializeInfo.FatherSerializeInfo != null) //有可能直接就是个基础类型 { if (nowSerializeInfo.FatherSerializeInfo.IsIDictionary) { if (nowSerializeInfo.FatherSerializeInfo.Index < nowSerializeInfo.FatherSerializeInfo.Length_FieldInfo / 2) { nowSerializeInfo.FatherSerializeInfo.Keys[nowSerializeInfo.FatherSerializeInfo.Index] = innerObject; } else { nowSerializeInfo.FatherSerializeInfo.SetInnerField(innerObject); } } else { nowSerializeInfo.FatherSerializeInfo.SetInnerField(innerObject); } nowSerializeInfo.FatherSerializeInfo.Index++; } else //有可能直接就是个基础类型 { fatherObject = innerObject; } bufStack.Pop(); if (fullLengthSchema) { if (readEnd >= totalLength) { break; } if (readEnd > brData.Position) { brData.SeekNextTo(readEnd); } } } else //非基础类 { isNull = brData.ReadByte(); if (isNull == Byte_Zero) //为null { if (nowSerializeInfo.FatherSerializeInfo == null) //直接就是null { fatherObject = null; } else { if (nowSerializeInfo.FatherSerializeInfo.IsIList) { nowSerializeInfo.FatherSerializeInfo.Index++; nowSerializeInfo.FatherSerializeInfo.SetInnerField(null); } else if (nowSerializeInfo.FatherSerializeInfo.IsIDictionary) { nowSerializeInfo.FatherSerializeInfo.SetInnerField(null); nowSerializeInfo.FatherSerializeInfo.Index++; } else { nowSerializeInfo.FatherSerializeInfo.Index++; } } bufStack.Pop(); if (fullLengthSchema) { if (readEnd >= totalLength) { break; } if (readEnd > brData.Position) { brData.SeekNextTo(readEnd); } } } else { if (nowSerializeInfo.TheType.IsArray) { nowSerializeInfo.IsArray = true; nowSerializeInfo.TheElementType = nowSerializeInfo.TheType.GetElementType(); nowSerializeInfo.Length_FieldInfo = (int)brData.ReadUInt32(); } else if (nowSerializeInfo.TheType.GetInterface("IList`1", false) != null) { nowSerializeInfo.IsIList = true; object[] ubas = nowSerializeInfo.TheType.GetCustomAttributes(false); if (ubas.Length > 0) { UseBaseArgument uba = ubas[0] as UseBaseArgument; if (uba != null) { nowSerializeInfo.TheElementType = nowSerializeInfo.TheType.BaseType.GetGenericArguments()[0]; } else { nowSerializeInfo.TheElementType = nowSerializeInfo.TheType.GetGenericArguments()[0]; } } else { nowSerializeInfo.TheElementType = nowSerializeInfo.TheType.GetGenericArguments()[0]; } nowSerializeInfo.Length_FieldInfo = (int)brData.ReadUInt32(); } else if (nowSerializeInfo.TheType.GetInterface("IDictionary`2", false) != null) { nowSerializeInfo.IsIDictionary = true; object[] ubas = nowSerializeInfo.TheType.GetCustomAttributes(false); if (ubas.Length > 0) { UseBaseArgument uba = ubas[0] as UseBaseArgument; if (uba != null) { nowSerializeInfo.TheElementType = nowSerializeInfo.TheType.BaseType.GetGenericArguments()[0]; nowSerializeInfo.TheValueType = nowSerializeInfo.TheType.BaseType.GetGenericArguments()[1]; } else { nowSerializeInfo.TheElementType = nowSerializeInfo.TheType.GetGenericArguments()[0]; nowSerializeInfo.TheValueType = nowSerializeInfo.TheType.GetGenericArguments()[1]; } } else { nowSerializeInfo.TheElementType = nowSerializeInfo.TheType.GetGenericArguments()[0]; nowSerializeInfo.TheValueType = nowSerializeInfo.TheType.GetGenericArguments()[1]; } nowSerializeInfo.Length_FieldInfo = (int)brData.ReadUInt32(); nowSerializeInfo.Keys = new object[nowSerializeInfo.Length_FieldInfo / 2]; } else { DynamicFieldInfo[] fis; lock (FI_Lookup) { if (FI_Lookup.ContainsKey(nowSerializeInfo.TheType)) { fis = FI_Lookup[nowSerializeInfo.TheType]; } else { PropertyInfo[] fi = nowSerializeInfo.TheType.GetProperties(AllInstanceBindingFlags).OrderBy(c => c.Name).ToArray(); List <DynamicFieldInfo> tempFis = new List <DynamicFieldInfo>(); for (int i = 0; i < fi.Length; i++) { PropertyInfo fiNow = fi[i]; object[] at = fiNow.GetCustomAttributes(typeof(System.NonSerializedAttribute), true); if (at.Length == 0) { tempFis.Add(new DynamicFieldInfo() { TheField = fiNow }); } } fis = tempFis.ToArray(); FI_Lookup.Add(nowSerializeInfo.TheType, fis); } } nowSerializeInfo.FieldInfos = fis; nowSerializeInfo.Length_FieldInfo = fis.Length; } if (!nowSerializeInfo.TheType.IsInterface) { nowSerializeInfo.CreatObject(); } if (fatherObject == null) { fatherObject = nowSerializeInfo.TheObject; } else { if (nowSerializeInfo.FatherSerializeInfo.IsIDictionary) { if (nowSerializeInfo.FatherSerializeInfo.Index < nowSerializeInfo.FatherSerializeInfo.Length_FieldInfo / 2) { //key值 nowSerializeInfo.FatherSerializeInfo.Keys[nowSerializeInfo.FatherSerializeInfo.Index] = nowSerializeInfo.TheObject; } else { //value值 nowSerializeInfo.FatherSerializeInfo.SetInnerField(nowSerializeInfo.TheObject); } } else { nowSerializeInfo.FatherSerializeInfo.SetInnerField(nowSerializeInfo.TheObject); } nowSerializeInfo.FatherSerializeInfo.Index++; } if (nowSerializeInfo.Length_FieldInfo != 0) { if (fullLengthSchema) { metablockLen = brData.TryReadMetaBlockLength(); nextSeekTo = brData.Position + metablockLen; //if (readEnd > 0 && readEnd < nextSeekTo) //{ // continue; //} } if (nowSerializeInfo.IsArray || nowSerializeInfo.IsIList) { innerSerializeInfo = new SerializeInfo(nowSerializeInfo.TheElementType, nextSeekTo); } else if (nowSerializeInfo.IsIDictionary) { innerSerializeInfo = new SerializeInfo(nowSerializeInfo.TheElementType, nextSeekTo); } else { DynamicFieldInfo tempDFI = nowSerializeInfo.FieldInfos[0]; innerSerializeInfo = new SerializeInfo(tempDFI.TheField.PropertyType, nextSeekTo); } innerSerializeInfo.FatherSerializeInfo = nowSerializeInfo; bufStack.Push(innerSerializeInfo); } } } } else //检查过 { if (nowSerializeInfo.Length_FieldInfo == nowSerializeInfo.Index) //检查完了 { bufStack.Pop(); if (fullLengthSchema) { if (readEnd >= totalLength) { break; } if (readEnd > 0 && readEnd > brData.Position) { brData.SeekNextTo(readEnd); } } } else { if (fullLengthSchema) { metablockLen = brData.TryReadMetaBlockLength(); nextSeekTo = brData.Position + metablockLen; if ((readEnd > 0 && readEnd < nextSeekTo)) { nowSerializeInfo.Index = nowSerializeInfo.Length_FieldInfo; brData.SeekAdd(-2); continue; } } if (nowSerializeInfo.IsArray || nowSerializeInfo.IsIList) { innerSerializeInfo = new SerializeInfo(nowSerializeInfo.TheElementType, nextSeekTo); } else if (nowSerializeInfo.IsIDictionary) { if (nowSerializeInfo.Index < nowSerializeInfo.Length_FieldInfo / 2) { innerSerializeInfo = new SerializeInfo(nowSerializeInfo.TheElementType, nextSeekTo); } else { innerSerializeInfo = new SerializeInfo(nowSerializeInfo.TheValueType, nextSeekTo); } } else { DynamicFieldInfo tempDFI = nowSerializeInfo.FieldInfos[nowSerializeInfo.Index]; innerSerializeInfo = new SerializeInfo(tempDFI.TheField.PropertyType, nextSeekTo); } innerSerializeInfo.FatherSerializeInfo = nowSerializeInfo; bufStack.Push(innerSerializeInfo); } } } if (fullLengthSchema) { readLength = totalLength; } else { readLength = brData.Position; } return(fatherObject); } catch (SerializeException se) { throw new SerializeException(objectType.ToString() + se.Message); } //catch (Exception e) { //throw new SerializeException(objectType.ToString() + "Data bytes and type not match" + e.Message); } }