public static string Serialize <T>(T message) { XMLOutStream xMLOutStream = new XMLOutStream(); xMLOutStream.Start("object"); SerializeObject(xMLOutStream, typeof(T), message); xMLOutStream.End(); return(xMLOutStream.Serialize()); }
private static void SerializeListElement(XMLOutStream stream, Type type, object message, int i) { //IL_0135: Unknown result type (might be due to invalid IL or missing references) //IL_014c: Unknown result type (might be due to invalid IL or missing references) //IL_0163: Unknown result type (might be due to invalid IL or missing references) //IL_017a: Unknown result type (might be due to invalid IL or missing references) //IL_0191: Unknown result type (might be due to invalid IL or missing references) switch (type.ToString()) { case "System.String": stream.Content("item", (string)message); break; case "System.Single": stream.Content("item", (float)message); break; case "System.Int32": stream.Content("item", (int)message); break; case "System.Boolean": stream.Content("item", (bool)message); break; case "UnityEngine.Vector3": stream.Content("item", (Vector3)message); break; case "UnityEngine.Quaternion": stream.Content("item", (Quaternion)message); break; case "UnityEngine.Color": stream.Content("item", (Color)message); break; case "UnityEngine.Rect": stream.Content("item", (Rect)message); break; case "UnityEngine.Vector2": stream.Content("item", (Vector2)message); break; default: stream.Start("item"); SerializeObject(stream, type, message); stream.End(); break; } }
//--------------------------------------------------------------------------------- // static Serialize //--------------------------------------------------------------------------------- public static string Serialize <T>(T message) { #if UNITY_FLASH && !UNITY_EDITOR Debug.LogError("XMLSerializer do not work with Flash !"); return(null); #else XMLOutStream stream = new XMLOutStream(); stream.Start("object"); SerializeObject(stream, typeof(T), message); stream.End(); return(stream.Serialize()); #endif }
//--------------------------------------------------------------------------------- // SerializeListElement //--------------------------------------------------------------------------------- private static void SerializeListElement(XMLOutStream stream, Type type, object message, int i) { switch (type.ToString()) { case "System.String": stream.Content("item", (string)message); break; case "System.Single": stream.Content("item", (float)message); break; case "System.Int32": stream.Content("item", (int)message); break; case "System.Boolean": stream.Content("item", (bool)message); break; case "UnityEngine.Vector3": stream.Content("item", (Vector3)message); break; case "UnityEngine.Quaternion": stream.Content("item", (Quaternion)message); break; case "UnityEngine.Color": stream.Content("item", (Color)message); break; case "UnityEngine.Rect": stream.Content("item", (Rect)message); break; case "UnityEngine.Vector2": stream.Content("item", (Vector2)message); break; default: stream.Start("item"); SerializeObject(stream, type, message); stream.End(); break; } }
//--------------------------------------------------------------------------------- // static SerializeObject //--------------------------------------------------------------------------------- private static void SerializeObject(XMLOutStream stream, Type type, object message) { System.Reflection.FieldInfo[] fieldInfo = type.GetFields(); foreach (System.Reflection.FieldInfo info in fieldInfo) { switch (info.FieldType.ToString()) { case "System.String": stream.Content(info.Name, (string)info.GetValue(message)); break; case "System.Single": stream.Content(info.Name, (float)info.GetValue(message)); break; case "System.Int32": stream.Content(info.Name, (int)info.GetValue(message)); break; case "System.Boolean": stream.Content(info.Name, (bool)info.GetValue(message)); break; case "UnityEngine.Vector3": stream.Content(info.Name, (Vector3)info.GetValue(message)); break; case "UnityEngine.Quaternion": stream.Content(info.Name, (Quaternion)info.GetValue(message)); break; case "UnityEngine.Color": stream.Content(info.Name, (Color)info.GetValue(message)); break; case "UnityEngine.Rect": stream.Content(info.Name, (Rect)info.GetValue(message)); break; case "UnityEngine.Vector2": stream.Content(info.Name, (Vector2)info.GetValue(message)); break; default: if (info.FieldType.IsEnum) // Enum { stream.Content(info.Name, (string)info.GetValue(message).ToString()); } else if (info.FieldType.IsGenericType) // List { Type containedType = info.FieldType.GetGenericArguments()[0]; Type typeList = typeof(List <>); Type actualType = typeList.MakeGenericType(containedType); PropertyInfo countMethod = actualType.GetProperty("Count"); PropertyInfo itemMethod = actualType.GetProperty("Item"); int count = (int)countMethod.GetValue(info.GetValue(message), new object[] {}); stream.Start(info.Name); for (int i = 0; i < count; ++i) { object o = itemMethod.GetValue(info.GetValue(message), new object[] { i }); SerializeListElement(stream, containedType, o, i); } stream.End(); } else if (info.FieldType.IsArray) // Array { object[] content = ToObjectArray((IEnumerable)info.GetValue(message)); Type containedType = Type.GetTypeArray(content)[0]; stream.Start(info.Name); for (int i = 0; i < content.Length; ++i) { object o = content[i]; SerializeListElement(stream, containedType, o, i); } stream.End(); } else // object { stream.Start(info.Name); SerializeObject(stream, info.FieldType, info.GetValue(message)); stream.End(); } break; } } }
private static void SerializeObject(XMLOutStream stream, Type type, object message) { //IL_0180: Unknown result type (might be due to invalid IL or missing references) //IL_019e: Unknown result type (might be due to invalid IL or missing references) //IL_01bc: Unknown result type (might be due to invalid IL or missing references) //IL_01da: Unknown result type (might be due to invalid IL or missing references) //IL_01f8: Unknown result type (might be due to invalid IL or missing references) FieldInfo[] fields = type.GetFields(); FieldInfo[] array = fields; foreach (FieldInfo fieldInfo in array) { switch (fieldInfo.FieldType.ToString()) { case "System.String": stream.Content(fieldInfo.Name, (string)fieldInfo.GetValue(message)); break; case "System.Single": stream.Content(fieldInfo.Name, (float)fieldInfo.GetValue(message)); break; case "System.Int32": stream.Content(fieldInfo.Name, (int)fieldInfo.GetValue(message)); break; case "System.Boolean": stream.Content(fieldInfo.Name, (bool)fieldInfo.GetValue(message)); break; case "UnityEngine.Vector3": stream.Content(fieldInfo.Name, (Vector3)fieldInfo.GetValue(message)); break; case "UnityEngine.Quaternion": stream.Content(fieldInfo.Name, (Quaternion)fieldInfo.GetValue(message)); break; case "UnityEngine.Color": stream.Content(fieldInfo.Name, (Color)fieldInfo.GetValue(message)); break; case "UnityEngine.Rect": stream.Content(fieldInfo.Name, (Rect)fieldInfo.GetValue(message)); break; case "UnityEngine.Vector2": stream.Content(fieldInfo.Name, (Vector2)fieldInfo.GetValue(message)); break; default: if (fieldInfo.FieldType.IsEnum) { stream.Content(fieldInfo.Name, fieldInfo.GetValue(message).ToString()); } else if (fieldInfo.FieldType.IsGenericType) { Type type2 = fieldInfo.FieldType.GetGenericArguments()[0]; Type typeFromHandle = typeof(List <>); Type type3 = typeFromHandle.MakeGenericType(type2); PropertyInfo property = type3.GetProperty("Count"); PropertyInfo property2 = type3.GetProperty("Item"); int num = (int)property.GetValue(fieldInfo.GetValue(message), new object[0]); stream.Start(fieldInfo.Name); for (int j = 0; j < num; j++) { object value = property2.GetValue(fieldInfo.GetValue(message), new object[1] { j }); SerializeListElement(stream, type2, value, j); } stream.End(); } else if (fieldInfo.FieldType.IsArray) { object[] array2 = ToObjectArray((IEnumerable)fieldInfo.GetValue(message)); Type type4 = Type.GetTypeArray(array2)[0]; stream.Start(fieldInfo.Name); for (int k = 0; k < array2.Length; k++) { object message2 = array2[k]; SerializeListElement(stream, type4, message2, k); } stream.End(); } else { stream.Start(fieldInfo.Name); SerializeObject(stream, fieldInfo.FieldType, fieldInfo.GetValue(message)); stream.End(); } break; } } }