/// <summary> /// 添加对象到流 /// </summary> /// <param name="graph">被序列化的对象</param> /// <param name="clazz">类型</param> /// <param name="writer">二进制流写入器</param> private static bool AddObjectToStream(object graph, Type clazz, BinaryWriter bw, AdvancedObjectMarshalAsAttribute attr) { if (TypeUtils.GetArrayElement(clazz) == null) { if (attr == null || !attr.NotNullFlags) { bw.Write(graph == null); } if (graph != null) { foreach (PropertyInfo pi in clazz.GetProperties()) { attr = AdvancedObjectMarshalAsAttribute.GetAttribute(pi); if (attr != null && attr.NotSerialized) // 如果不可序列化则迭代到下一个 { continue; } object value = pi.GetValue(graph, null); AdvancedObjectFormatter.AddValueToStream(pi.PropertyType, value, attr, bw); } } return(true); } return(false); }
/// <summary> /// 获取具体大小 /// </summary> /// <param name="attr">特性</param> /// <param name="clazz">类型</param> /// <returns></returns> private static int GetSpecificSize(AdvancedObjectMarshalAsAttribute attr, Type clazz) { if (attr != null && attr.SizeConst > 0) { return(attr.SizeConst); } return(ValueTypeUtils.SizeBy(clazz)); }
/// <summary> /// 获取一个有效的的字符串编码 /// </summary> /// <param name="attr">转换的特性类型</param> /// <returns></returns> private static Encoding GetTextEncoding(AdvancedObjectMarshalAsAttribute attr) { if (attr == null) { return(Encoding.Default); } return(Encoding.GetEncoding(attr.TextEncoding)); }
/// <summary> /// 添加列表到流中 /// </summary> private static bool AddListToStream(Type clazz, IList value, AdvancedObjectMarshalAsAttribute attr, BinaryWriter bw) { Type element = TypeUtils.GetArrayElement(clazz); if (element != null) { if (value == null) { if (attr == null || attr.ArrayDoubleLength) { bw.Write(AdvancedObjectFormatter.As(BitConverter.GetBytes((short)-1), attr)); } else { bw.Write((sbyte)-1); } } else { int size = value.Count; if (attr != null && attr.SizeConst > 0) { size = attr.SizeConst; } else if (attr == null || attr.ArrayDoubleLength) { bw.Write(AdvancedObjectFormatter.As(BitConverter.GetBytes((short)size), attr)); } else { bw.Write((sbyte)size); } if (TypeUtils.IsBuffer(clazz)) { bw.Write((byte[])value); } else { bool basic = ValueTypeUtils.IsBasicType(element); for (int i = 0; i < size; i++) { object graph = value[i]; if (basic) { AdvancedObjectFormatter.AddValueToStream(element, graph, attr, bw); } else { AdvancedObjectFormatter.AddObjectToStream(graph, element, bw, attr); } } } } return(true); } return(false); }
/// <summary> /// 对二进制流进行变换 /// </summary> /// <param name="buffer">可能被变换的二进制流</param> /// <param name="attr">变换二进制流的依据</param> /// <returns></returns> private static byte[] As(byte[] buffer, AdvancedObjectMarshalAsAttribute attr) { if (buffer == null || attr == null) { return(buffer); } if (attr.BigEndianModel) { Array.Reverse(buffer); } return(buffer); }
/// <summary> /// 添加值到流中 /// </summary> private static bool AddValueToStream(Type clazz, object value, AdvancedObjectMarshalAsAttribute attr, BinaryWriter bw) { int size = ValueTypeUtils.SizeBy(clazz); if (size > 0) { if (ValueTypeUtils.IsFloatType(clazz)) { bw.Write(AdvancedObjectFormatter.As(ValueTypeUtils.BinaryBy(clazz, Convert.ToDouble(value)), attr)); } else { if (ValueTypeUtils.IsDateTime(clazz)) { value = Convert.ToDateTime(value).Ticks; } if (ValueTypeUtils.IsULong(clazz)) { value = unchecked ((long)(ulong)value); } if (ValueTypeUtils.IsIPAddress(clazz)) { value = BitConverter.ToInt32(((IPAddress)value).GetAddressBytes(), 0); } bw.Write(AdvancedObjectFormatter.As(ValueTypeUtils.BinaryBy(clazz, Convert.ToInt64(value), size), attr)); } return(true); } else { if (AdvancedObjectFormatter.AddStringToStream(clazz, value as string, attr, bw)) { return(true); } if (AdvancedObjectFormatter.AddListToStream(clazz, value as IList, attr, bw)) { return(true); } if (AdvancedObjectFormatter.AddObjectToStream(value, clazz, bw, attr)) { return(true); } } return(false); }
/// <summary> /// 添加字符串到流中 /// </summary> private static bool AddStringToStream(Type clazz, string value, AdvancedObjectMarshalAsAttribute attr, BinaryWriter bw) { if (TypeUtils.IsString(clazz)) { if (value == null) { if (attr == null || attr.ArrayDoubleLength) { bw.Write(AdvancedObjectFormatter.As(BitConverter.GetBytes((short)-1), attr)); } else { bw.Write((sbyte)-1); } } else { byte[] buffer = AdvancedObjectFormatter.GetTextEncoding(attr).GetBytes(value); int size = buffer.Length; if (attr != null && attr.SizeConst > 0) { size = attr.SizeConst; } else if (attr == null || attr.ArrayDoubleLength) { bw.Write(AdvancedObjectFormatter.As(BitConverter.GetBytes((short)size), attr)); } else { bw.Write((sbyte)size); } bw.Write(buffer, 0, size); } return(true); } return(false); }