/// <summary> /// 反序列化分析 /// </summary> /// <param name="type">分析类型</param> /// <param name="reader">读取器</param> /// <param name="attribute"></param> /// <param name="encoding">编码类型(默认UTF8)</param> /// <returns>反序列化得到的对象</returns> private static Object DeserializeAnalyse(Type type, BitReader reader, BitFormatElementAttribute attribute, Encoding encoding) { if (type.IsInterface) { return(null); } Object value; if (DeserializeByAttribute(reader, attribute, out value)) { return(value); } if (TypeIsString(type)) { return(DeserializeString(reader, attribute)); } else if (type.IsArray) { return(DeserializeArray(type, reader, attribute, encoding)); } else if (type.IsClass) { return(DeserializeClass(type, reader)); } else if (TypeIsStruct(type)) { return(DeserializeClass(type, reader)); } else { return(DeserializeValue(type, reader, encoding)); } }
/// <summary> /// 分析需要哪种序列化 /// </summary> /// <param name="type"></param> /// <param name="value"></param> /// <param name="writer"></param> /// <param name="attribute"></param> /// <param name="encoding"></param> private void SerializeAnalyse(Type type, Object value, BitWriter writer, BitFormatElementAttribute attribute, Encoding encoding) { if (type.IsInterface) { return; } if (SerializeByAttribute(attribute, writer, value)) { return; } if (TypeIsString(type)) { SerializeString(writer, (String)value, attribute, encoding); } else if (type.IsArray) { SerializeArray(value, writer, attribute, encoding); } else if (type.IsClass) { SerializeClass(type, value, writer, encoding); } else if (TypeIsStruct(type)) { SerializeClass(type, value, writer, encoding); } else { SerializeValue(type, value, writer, encoding); } }
private static Boolean DeserializeByAttribute(BitReader reader, BitFormatElementAttribute attribute, out Object value) { value = null; switch (attribute.Type) { case BitType.Bits: if (attribute.Length <= 0) { throw new ArgumentOutOfRangeException("length"); } value = reader.ReadBits(attribute.Length); return(true); case BitType.GolombUE32: throw new InvalidOperationException("GolombUE32"); //return true; case BitType.GolombSE32: throw new InvalidOperationException("GolombSE32"); //return true; default: return(false); } }
/// <summary> /// 反序列化字符串 /// </summary> /// <param name="reader">读取器</param> /// <param name="attribute"></param> /// <returns>字符串</returns> private static String DeserializeString(BitReader reader, BitFormatElementAttribute attribute) { if (attribute.Length <= 0) { throw new ArgumentOutOfRangeException("length"); } return(reader.ReadString(attribute.Length)); }
/// <summary> /// 序列化String对象 /// </summary> /// <param name="writer"></param> /// <param name="value"></param> /// <param name="attribute"></param> /// <param name="encoding"></param> private static void SerializeString(BitWriter writer, String value, BitFormatElementAttribute attribute, Encoding encoding) { if (value == null) { return; } if (attribute.Length <= 0) { throw new ArgumentOutOfRangeException("length"); } writer.Write(value, attribute.Length); }
/// <summary> /// 序列化数组 /// </summary> /// <param name="value"></param> /// <param name="writer"></param> /// <param name="attribute"></param> /// <param name="encoding"></param> private void SerializeArray(Object value, BitWriter writer, BitFormatElementAttribute attribute, Encoding encoding) { Array vs = (Array)value; if (attribute.Length <= 0) { throw new ArgumentOutOfRangeException("length"); } for (int j = 0; j < attribute.Length; j++) { var v = vs.GetValue(j); SerializeAnalyse(v.GetType(), v, writer, attribute, encoding); } }
/// <summary> /// 反序列化数组 /// </summary> /// <param name="type">序列化类型</param> /// <param name="reader">读取器</param> /// <param name="attribute"></param> /// <param name="encoding">编码类型(默认UTF8)</param> /// <returns>数组</returns> private static Array DeserializeArray(Type type, BitReader reader, BitFormatElementAttribute attribute, Encoding encoding) { if (attribute.Length <= 0) { throw new ArgumentOutOfRangeException("length"); } Array vs = Array.CreateInstance(type.GetElementType(), attribute.Length); for (int i = 0; i < vs.Length; i++) { var v = DeserializeAnalyse(type.GetElementType(), reader, attribute, encoding); if (type.GetElementType().IsEnum) { v = GetEnumValue(type.GetElementType(), v); } vs.SetValue(v, i); } return(vs); }
/// <summary> /// 获取对象下所有属性 /// </summary> /// <param name="o"></param> /// <param name="types"></param> /// <param name="values"></param> /// <param name="attributes"></param> private void GetProperties(Object o, out Type[] types, out Object[] values, out BitFormatElementAttribute[] attributes) { var ps = m_Type.GetProperties(); ps = ps.Where(x => GetBitFormatElementAttribute(x) != null).OrderBy(x => GetBitFormatElementAttribute(x).Order).ToArray(); types = new Type[ps.Length]; values = new Object[ps.Length]; attributes = new BitFormatElementAttribute[ps.Length]; for (int i = 0; i < ps.Length; i++) { attributes[i] = GetBitFormatElementAttribute(ps[i]); values[i] = ps[i].GetValue(o, null); if (!attributes[i].IsNullable && values[i] == null) { throw new ArgumentNullException(ps[i].Name); } types[i] = ps[i].PropertyType; } }
private Boolean SerializeByAttribute(BitFormatElementAttribute attribute, BitWriter writer, Object value) { switch (attribute.Type) { case BitType.Bits: if (attribute.Length <= 0) { throw new ArgumentOutOfRangeException("length"); } writer.WriteBits((Int32)value, attribute.Length); return(true); case BitType.GolombUE32: throw new InvalidOperationException("GolombUE32"); //return true; case BitType.GolombSE32: throw new InvalidOperationException("GolombSE32"); //return true; default: return(false); } }