/// <summary> /// 核心序列化代码 /// </summary> private static void Serialize(FGWrite write, object ob, Type type) { // 基本类型 if (type.IsPrimitive) { if (type.Equals(typeof(int))) { write.Write((int)ob); } else if (type.Equals(typeof(float))) { write.Write((float)ob); } else if (type.Equals(typeof(byte))) { write.Write((byte)ob); } else if (type.Equals(typeof(long))) { write.Write((long)ob); } else { HelperTool.LogError("FGSerializer.Deserialize : 序列化类型不支持"); } return; } // 字符串 if (type.Equals(typeof(string))) { string str = (string)ob; write.Write(str.Length); write.WriteString(str); return; } // 数组 if (type.IsArray) { Array arr = (Array)ob; if (arr != null) { if (arr.Length > 0) { write.Write(arr.Length); Type at = arr.GetValue(0).GetType(); if (at.Equals(typeof(byte)) || at.Equals(typeof(sbyte))) { write.Write((byte[])ob); } else { for (int i = 0; i < arr.Length; i++) { Serialize(write, arr.GetValue(i), at); } } } } return; } // 泛型List if (type.IsGenericType) { Type listType = type.GetGenericArguments()[0]; Type[] types = type.GetInterfaces(); if (Array.IndexOf(types, typeof(IEnumerable)) > -1) { IEnumerable en = ob as IEnumerable; var enumerator = en.GetEnumerator(); int length = 0; while (enumerator.MoveNext()) { length++; } write.Write(length); enumerator = en.GetEnumerator(); while (enumerator.MoveNext()) { Serialize(write, enumerator.Current, listType); } } return; } // 类 FGReflection reflection = new FGReflection(type); foreach (FGSerializeInfo item in reflection.fieldList) { FieldInfo info = item.info; Type t = info.FieldType; Serialize(write, info.GetValue(ob), t); } }
private static object Deserialize(FGReader reader, Type type) { // 基本类型 if (type.IsPrimitive) { return(DeserializePrimitive(reader, type)); } else if (type.Equals(typeof(string))) { return(reader.ReadString()); } /* 自定义特性类反序列化 */ //Dictionary<string, int> lenDict = new Dictionary<string, int>(); //保存 数组长度标志 和 数组 的关系 object ob = Activator.CreateInstance(type, true); FGReflection reflection = new FGReflection(type); foreach (var item in reflection.fieldList) { FieldInfo info = item.info; Type t = info.FieldType; if (t.Equals(typeof(string))) { int length = reader.ReadInt(); if (length > 0) { info.SetValue(ob, reader.ReadString(length)); } else { info.SetValue(ob, reader.ReadString()); } } else { // 数组 if (t.IsArray) { //根据长度记录,序列化 int length = reader.ReadInt(); if (length > 0) { Type at = t.GetElementType(); if (at.Equals(typeof(byte)) || at.Equals(typeof(sbyte))) { byte[] array = reader.ReadBytes(length); info.SetValue(ob, array); } else { Array array = Array.CreateInstance(t.GetElementType(), length); for (int i = 0; i < length; i++) { array.SetValue(Deserialize(reader, at), i); } info.SetValue(ob, array); } } else { HelperTool.LogError("FGSerializer.Deserialize : 反序列化" + type.Name + "失败"); continue; } } // 泛型List else if (t.IsGenericType) { Type listType = t.GetGenericArguments()[0]; Type[] types = t.GetInterfaces(); if (Array.IndexOf(types, typeof(IEnumerable)) > -1) { int length = reader.ReadInt(); Type lsType = typeof(List <>); var makeme = lsType.MakeGenericType(listType); object o = Activator.CreateInstance(makeme); MethodInfo method = o.GetType().GetMethod("Add"); object[] insts = new object[length]; for (int i = 0; i < length; i++) { Object inst = Deserialize(reader, listType); method.Invoke(o, new object[] { inst }); } info.SetValue(ob, o); } } else { //非数组结构直接序列化,如果字段为数组的长度标志,记录下来 info.SetValue(ob, DeserializePrimitive(reader, t)); } } } return(ob); }