public static void SerializeArray(MemoryStream stream, Array array) { Type type = array.GetType(); Type eType = type.GetElementType(); if (eType == typeof(object)) { SerializeObjectArray(stream, array); } else { // 指定类型的数组 // 标记数组类型 stream.WriteByte((byte)SUType.Array); SUType eTypeCode = GetCodeByType(eType); if (eTypeCode != SUType.Unknown) { SerializePrimaryArray(stream, array, eTypeCode); } else { if (typeDict.TryGetValue(eType, out CustomType customType)) { SerializeCustomTypeArray(stream, array, customType); } else { throw new Exception(string.Format("not support type: {0}", eType)); } } } }
public static Type GetTypeByCode(SUType code) { switch (code) { case SUType.Byte: return(typeof(byte)); case SUType.Bool: return(typeof(bool)); case SUType.Short: return(typeof(short)); case SUType.Int: return(typeof(int)); case SUType.Float: return(typeof(float)); case SUType.Double: return(typeof(double)); case SUType.String: return(typeof(string)); default: throw new Exception(string.Format("not support: {0}", code)); } }
public static object Deserialize(MemoryStream stream, SUType type) { if (type == SUType.Bool) { return(DeserializeBool(stream)); } else if (type == SUType.Byte) { return(DeserializeByte(stream)); } else if (type == SUType.Short) { return(DeserializeShort(stream)); } else if (type == SUType.Int) { return(DeserializeInt(stream)); } else if (type == SUType.Float) { return(DeserializeFloat(stream)); } else if (type == SUType.Double) { return(DeserializeDouble(stream)); } else if (type == SUType.String) { return(DeserializeString(stream)); } else if (type == SUType.Array) { return(DeserializeArray(stream)); } //else if (type == SUType.ObjectArray) { // return DeserializeObjectArray(stream); //} else if (type == SUType.List) { return(DeserializeList(stream)); } //else if (type == SUType.ObjectList) { // return DeserializeObjectList(stream); //} else if (type == SUType.Dictionary) { return(DeserializeDictionary(stream)); } else if (type == SUType.CustomClass) { byte typeCode = (byte)stream.ReadByte(); CustomType customType = null; if (typeCodeDict.TryGetValue(typeCode, out customType)) { return(customType.DeserializationFunc.Invoke(stream)); } return(null); } return(null); }
public static IList DeserializeList(MemoryStream stream) { SUType eTypeCode = (SUType)stream.ReadByte(); if (eTypeCode == SUType.CustomClass) { byte customTypeCode = (byte)stream.ReadByte(); if (typeCodeDict.TryGetValue(customTypeCode, out CustomType customType)) { Type type = typeof(List <>).MakeGenericType(customType.Type); IList list = Activator.CreateInstance(type) as IList; int length = DeserializeLength(stream); for (int i = 0; i < length; i++) { object v = customType.DeserializationFunc.Invoke(stream); list.Add(v); } return(list); } else { throw new Exception(string.Format("not support type: {0}", customTypeCode)); } } else { if (eTypeCode == SUType.Dictionary) { SUType kTypeCode = (SUType)stream.ReadByte(); SUType vTypeCode = (SUType)stream.ReadByte(); Type kType = GetTypeByCode(kTypeCode); Type vType = GetTypeByCode(vTypeCode); int length = DeserializeLength(stream); Type dictType = typeof(Dictionary <,>).MakeGenericType(kType, vType); Type type = typeof(List <>).MakeGenericType(dictType); IList list = Activator.CreateInstance(type) as IList; for (int i = 0; i < length; i++) { object v = DeserializeDictionary(stream) as IDictionary; list.Add(v); } return(list); } else { Type eType = GetTypeByCode(eTypeCode); Type type = typeof(List <>).MakeGenericType(eType); IList list = Activator.CreateInstance(type) as IList; int length = DeserializeLength(stream); for (int i = 0; i < length; i++) { object v = Deserialize(stream, eTypeCode); list.Add(v); } return(list); } } }
public static void SerializePrimaryList(MemoryStream stream, IList list, SUType eTypeCode) { // 标记数组元素类型 SerializeByte(stream, (byte)eTypeCode); // 标记数组长度 SerializeInt(stream, list.Count); // 序列化数组元素 for (int i = 0; i < list.Count; i++) { object v = list[i]; Serialize(stream, v); } }
public static void SerializePrimaryArray(MemoryStream stream, Array array, SUType eTypeCode) { // 标记数组元素类型 SerializeByte(stream, (byte)eTypeCode); // 标记数组长度 SerializeInt(stream, array.Length); // 序列化数组元素 for (int i = 0; i < array.Length; i++) { object v = array.GetValue(i); Serialize(stream, v); } }
public static IDictionary DeserializeDictionary(MemoryStream stream) { SUType kTypeCode = (SUType)DeserializeByte(stream); Type kType = GetTypeByCode(kTypeCode); SUType vTypeCode = (SUType)DeserializeByte(stream); Type vType = GetTypeByCode(vTypeCode); Type type = typeof(Dictionary <,>).MakeGenericType(kType, vType); IDictionary dictionary = Activator.CreateInstance(type) as IDictionary; int length = DeserializeLength(stream); for (int i = 0; i < length; i++) { object k = Deserialize(stream, kTypeCode); object v = Deserialize(stream, vTypeCode); dictionary.Add(k, v); } return(dictionary); }
public static void SerializeDictionaryList(MemoryStream stream, IList list) { SerializeByte(stream, (byte)SUType.Dictionary); Type type = list.GetType(); Type dictType = type.GetGenericArguments()[0]; Type kType = dictType.GetGenericArguments()[0]; Type vType = dictType.GetGenericArguments()[1]; SUType kTypeCode = GetCodeByType(kType); SUType vTypeCode = GetCodeByType(vType); SerializeByte(stream, (byte)kTypeCode); SerializeByte(stream, (byte)vTypeCode); SerializeInt(stream, list.Count); for (int i = 0; i < list.Count; i++) { object v = list[i]; SerializeDictionary(stream, v as IDictionary); } }
public static void SerializeList(MemoryStream stream, IList list) { Type type = list.GetType(); Type eType = type.GetGenericArguments()[0]; if (eType == typeof(object)) { SerializeObjectList(stream, list as List <object>); } else { stream.WriteByte((byte)SUType.List); SUType eTypeCode = GetCodeByType(eType); if (eTypeCode != SUType.Unknown) { if (eTypeCode == SUType.Dictionary) { SerializeDictionaryList(stream, list); } else { SerializePrimaryList(stream, list, eTypeCode); } } else { if (typeDict.TryGetValue(eType, out CustomType customType)) { SerializeCustomTypeList(stream, list, customType); } else { throw new Exception(string.Format("not support type: {0}", eType)); } } } }
public static Array DeserializeArray(MemoryStream stream) { SUType eTypeCode = (SUType)stream.ReadByte(); if (eTypeCode == SUType.CustomClass) { byte customTypeCode = (byte)stream.ReadByte(); if (typeCodeDict.TryGetValue(customTypeCode, out CustomType customType)) { int length = DeserializeLength(stream); Array array = Array.CreateInstance(customType.Type, length); for (int i = 0; i < length; i++) { object v = customType.DeserializationFunc.Invoke(stream); array.SetValue(v, i); } return(array); } else { throw new Exception(string.Format("not support type: {0}", customTypeCode)); } } else { Type eType = GetTypeByCode(eTypeCode); int length = DeserializeLength(stream); Array array = Array.CreateInstance(eType, length); for (int i = 0; i < length; i++) { object v = Deserialize(stream, eTypeCode); array.SetValue(v, i); } return(array); } }
public static object Deserialize(MemoryStream stream) { SUType type = (SUType)stream.ReadByte(); return(Deserialize(stream, type)); }