public static T ObjectFromXml <T>(XmlNode xmlRoot, bool doPostLoad) { XmlAttribute xmlAttribute = xmlRoot.Attributes["IsNull"]; if (xmlAttribute != null && xmlAttribute.Value.ToUpperInvariant() == "TRUE") { return(default(T)); } MethodInfo methodInfo = CustomDataLoadMethodOf(typeof(T)); if (methodInfo != null) { xmlRoot = XmlInheritance.GetResolvedNodeFor(xmlRoot); Type type = ClassTypeOf <T>(xmlRoot); currentlyInstantiatingObjectOfType.Push(type); T val; try { val = (T)Activator.CreateInstance(type); } finally { currentlyInstantiatingObjectOfType.Pop(); } try { methodInfo.Invoke(val, new object[1] { xmlRoot }); } catch (Exception ex) { Log.Error(string.Concat("Exception in custom XML loader for ", typeof(T), ". Node is:\n ", xmlRoot.OuterXml, "\n\nException is:\n ", ex.ToString())); val = default(T); } if (doPostLoad) { TryDoPostLoad(val); } return(val); } if (typeof(ISlateRef).IsAssignableFrom(typeof(T))) { try { return(ParseHelper.FromString <T>(InnerTextWithReplacedNewlinesOrXML(xmlRoot))); } catch (Exception ex2) { Log.Error(string.Concat("Exception parsing ", xmlRoot.OuterXml, " to type ", typeof(T), ": ", ex2)); } return(default(T)); } if (xmlRoot.ChildNodes.Count == 1 && xmlRoot.FirstChild.NodeType == XmlNodeType.CDATA) { if (typeof(T) != typeof(string)) { Log.Error("CDATA can only be used for strings. Bad xml: " + xmlRoot.OuterXml); return(default(T)); } return((T)(object)xmlRoot.FirstChild.Value); } if (xmlRoot.ChildNodes.Count == 1 && xmlRoot.FirstChild.NodeType == XmlNodeType.Text) { try { return(ParseHelper.FromString <T>(xmlRoot.InnerText)); } catch (Exception ex3) { Log.Error(string.Concat("Exception parsing ", xmlRoot.OuterXml, " to type ", typeof(T), ": ", ex3)); } return(default(T)); } if (Attribute.IsDefined(typeof(T), typeof(FlagsAttribute))) { List <T> list = ListFromXml <T>(xmlRoot); int num = 0; foreach (T item in list) { int num2 = (int)(object)item; num |= num2; } return((T)(object)num); } if (typeof(T).HasGenericDefinition(typeof(List <>))) { Func <XmlNode, object> value = null; if (!listFromXmlMethods.TryGetValue(typeof(T), out value)) { MethodInfo method = typeof(DirectXmlToObject).GetMethod("ListFromXmlReflection", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); Type[] genericArguments = typeof(T).GetGenericArguments(); value = (Func <XmlNode, object>)Delegate.CreateDelegate(typeof(Func <XmlNode, object>), method.MakeGenericMethod(genericArguments)); listFromXmlMethods.Add(typeof(T), value); } return((T)value(xmlRoot)); } if (typeof(T).HasGenericDefinition(typeof(Dictionary <, >))) { Func <XmlNode, object> value2 = null; if (!dictionaryFromXmlMethods.TryGetValue(typeof(T), out value2)) { MethodInfo method2 = typeof(DirectXmlToObject).GetMethod("DictionaryFromXmlReflection", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); Type[] genericArguments2 = typeof(T).GetGenericArguments(); value2 = (Func <XmlNode, object>)Delegate.CreateDelegate(typeof(Func <XmlNode, object>), method2.MakeGenericMethod(genericArguments2)); dictionaryFromXmlMethods.Add(typeof(T), value2); } return((T)value2(xmlRoot)); } if (!xmlRoot.HasChildNodes) { if (typeof(T) == typeof(string)) { return((T)(object)""); } XmlAttribute xmlAttribute2 = xmlRoot.Attributes["IsNull"]; if (xmlAttribute2 != null && xmlAttribute2.Value.ToUpperInvariant() == "TRUE") { return(default(T)); } if (typeof(T).IsGenericType) { Type genericTypeDefinition = typeof(T).GetGenericTypeDefinition(); if (genericTypeDefinition == typeof(List <>) || genericTypeDefinition == typeof(HashSet <>) || genericTypeDefinition == typeof(Dictionary <, >)) { return(Activator.CreateInstance <T>()); } } } xmlRoot = XmlInheritance.GetResolvedNodeFor(xmlRoot); Type type2 = ClassTypeOf <T>(xmlRoot); Type type3 = Nullable.GetUnderlyingType(type2) ?? type2; currentlyInstantiatingObjectOfType.Push(type3); T val2; try { val2 = (T)Activator.CreateInstance(type3); } finally { currentlyInstantiatingObjectOfType.Pop(); } HashSet <string> hashSet = null; if (xmlRoot.ChildNodes.Count > 1) { hashSet = new HashSet <string>(); } for (int i = 0; i < xmlRoot.ChildNodes.Count; i++) { XmlNode xmlNode = xmlRoot.ChildNodes[i]; if (xmlNode is XmlComment) { continue; } if (xmlRoot.ChildNodes.Count > 1) { if (hashSet.Contains(xmlNode.Name)) { Log.Error(string.Concat("XML ", typeof(T), " defines the same field twice: ", xmlNode.Name, ".\n\nField contents: ", xmlNode.InnerText, ".\n\nWhole XML:\n\n", xmlRoot.OuterXml)); } else { hashSet.Add(xmlNode.Name); } } FieldInfo value3 = null; DeepProfiler.Start("GetFieldInfoForType"); try { value3 = GetFieldInfoForType(val2.GetType(), xmlNode.Name, xmlRoot); } finally { DeepProfiler.End(); } if (value3 == null) { DeepProfiler.Start("Field search"); try { FieldAliasCache key = new FieldAliasCache(val2.GetType(), xmlNode.Name); if (!fieldAliases.TryGetValue(key, out value3)) { FieldInfo[] fields = val2.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (FieldInfo fieldInfo in fields) { object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(LoadAliasAttribute), inherit: true); for (int k = 0; k < customAttributes.Length; k++) { if (((LoadAliasAttribute)customAttributes[k]).alias.EqualsIgnoreCase(xmlNode.Name)) { value3 = fieldInfo; break; } } if (value3 != null) { break; } } fieldAliases.Add(key, value3); } } finally { DeepProfiler.End(); } } if (value3 != null && value3.TryGetAttribute <UnsavedAttribute>() != null && !value3.TryGetAttribute <UnsavedAttribute>().allowLoading) { Log.Error("XML error: " + xmlNode.OuterXml + " corresponds to a field in type " + val2.GetType().Name + " which has an Unsaved attribute. Context: " + xmlRoot.OuterXml); } else if (value3 == null) { DeepProfiler.Start("Field search 2"); try { bool flag = false; XmlAttribute xmlAttribute3 = xmlNode.Attributes?["IgnoreIfNoMatchingField"]; if (xmlAttribute3 != null && xmlAttribute3.Value.ToUpperInvariant() == "TRUE") { flag = true; } else { object[] customAttributes = val2.GetType().GetCustomAttributes(typeof(IgnoreSavedElementAttribute), inherit: true); for (int j = 0; j < customAttributes.Length; j++) { if (string.Equals(((IgnoreSavedElementAttribute)customAttributes[j]).elementToIgnore, xmlNode.Name, StringComparison.OrdinalIgnoreCase)) { flag = true; break; } } } if (!flag) { Log.Error("XML error: " + xmlNode.OuterXml + " doesn't correspond to any field in type " + val2.GetType().Name + ". Context: " + xmlRoot.OuterXml); } } finally { DeepProfiler.End(); } } else if (typeof(Def).IsAssignableFrom(value3.FieldType)) { if (xmlNode.InnerText.NullOrEmpty()) { value3.SetValue(val2, null); continue; } XmlAttribute xmlAttribute4 = xmlNode.Attributes["MayRequire"]; DirectXmlCrossRefLoader.RegisterObjectWantsCrossRef(val2, value3, xmlNode.InnerText, xmlAttribute4?.Value.ToLower()); } else { object obj = null; try { obj = GetObjectFromXmlMethod(value3.FieldType)(xmlNode, doPostLoad); } catch (Exception ex4) { Log.Error("Exception loading from " + xmlNode.ToString() + ": " + ex4.ToString()); continue; } if (!typeof(T).IsValueType) { value3.SetValue(val2, obj); continue; } object obj2 = val2; value3.SetValue(obj2, obj); val2 = (T)obj2; } } if (doPostLoad) { TryDoPostLoad(val2); } return(val2); }
public static T ObjectFromXml <T>(XmlNode xmlRoot, bool doPostLoad) where T : new() { MethodInfo methodInfo = DirectXmlToObject.CustomDataLoadMethodOf(typeof(T)); T result; if (methodInfo != null) { xmlRoot = XmlInheritance.GetResolvedNodeFor(xmlRoot); Type type = DirectXmlToObject.ClassTypeOf <T>(xmlRoot); DirectXmlToObject.currentlyInstantiatingObjectOfType.Push(type); T t; try { t = (T)((object)Activator.CreateInstance(type)); } finally { DirectXmlToObject.currentlyInstantiatingObjectOfType.Pop(); } try { methodInfo.Invoke(t, new object[] { xmlRoot }); } catch (Exception ex) { Log.Error(string.Concat(new object[] { "Exception in custom XML loader for ", typeof(T), ". Node is:\n ", xmlRoot.OuterXml, "\n\nException is:\n ", ex.ToString() }), false); t = default(T); } if (doPostLoad) { DirectXmlToObject.TryDoPostLoad(t); } result = t; } else if (xmlRoot.ChildNodes.Count == 1 && xmlRoot.FirstChild.NodeType == XmlNodeType.CDATA) { if (typeof(T) != typeof(string)) { Log.Error("CDATA can only be used for strings. Bad xml: " + xmlRoot.OuterXml, false); result = default(T); } else { result = (T)((object)xmlRoot.FirstChild.Value); } } else if (xmlRoot.ChildNodes.Count == 1 && xmlRoot.FirstChild.NodeType == XmlNodeType.Text) { try { return((T)((object)ParseHelper.FromString(xmlRoot.InnerText, typeof(T)))); } catch (Exception ex2) { Log.Error(string.Concat(new object[] { "Exception parsing ", xmlRoot.OuterXml, " to type ", typeof(T), ": ", ex2 }), false); } result = default(T); } else if (Attribute.IsDefined(typeof(T), typeof(FlagsAttribute))) { List <T> list = DirectXmlToObject.ListFromXml <T>(xmlRoot); int num = 0; foreach (T t2 in list) { int num2 = (int)((object)t2); num |= num2; } result = (T)((object)num); } else if (typeof(T).HasGenericDefinition(typeof(List <>))) { MethodInfo method = typeof(DirectXmlToObject).GetMethod("ListFromXml", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); Type[] genericArguments = typeof(T).GetGenericArguments(); MethodInfo methodInfo2 = method.MakeGenericMethod(genericArguments); object[] parameters = new object[] { xmlRoot }; object obj = methodInfo2.Invoke(null, parameters); result = (T)((object)obj); } else if (typeof(T).HasGenericDefinition(typeof(Dictionary <, >))) { MethodInfo method2 = typeof(DirectXmlToObject).GetMethod("DictionaryFromXml", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); Type[] genericArguments2 = typeof(T).GetGenericArguments(); MethodInfo methodInfo3 = method2.MakeGenericMethod(genericArguments2); object[] parameters2 = new object[] { xmlRoot }; object obj2 = methodInfo3.Invoke(null, parameters2); result = (T)((object)obj2); } else { if (!xmlRoot.HasChildNodes) { if (typeof(T) == typeof(string)) { return((T)((object)"")); } XmlAttribute xmlAttribute = xmlRoot.Attributes["IsNull"]; if (xmlAttribute != null && xmlAttribute.Value.ToUpperInvariant() == "TRUE") { return(default(T)); } if (typeof(T).IsGenericType) { Type genericTypeDefinition = typeof(T).GetGenericTypeDefinition(); if (genericTypeDefinition == typeof(List <>) || genericTypeDefinition == typeof(HashSet <>) || genericTypeDefinition == typeof(Dictionary <, >)) { return(Activator.CreateInstance <T>()); } } } xmlRoot = XmlInheritance.GetResolvedNodeFor(xmlRoot); Type type2 = DirectXmlToObject.ClassTypeOf <T>(xmlRoot); Type type3 = Nullable.GetUnderlyingType(type2) ?? type2; DirectXmlToObject.currentlyInstantiatingObjectOfType.Push(type3); T t3; try { t3 = (T)((object)Activator.CreateInstance(type3)); } finally { DirectXmlToObject.currentlyInstantiatingObjectOfType.Pop(); } List <string> list2 = null; if (xmlRoot.ChildNodes.Count > 1) { list2 = new List <string>(); } for (int i = 0; i < xmlRoot.ChildNodes.Count; i++) { XmlNode xmlNode = xmlRoot.ChildNodes[i]; if (!(xmlNode is XmlComment)) { if (xmlRoot.ChildNodes.Count > 1) { if (list2.Contains(xmlNode.Name)) { Log.Error(string.Concat(new object[] { "XML ", typeof(T), " defines the same field twice: ", xmlNode.Name, ".\n\nField contents: ", xmlNode.InnerText, ".\n\nWhole XML:\n\n", xmlRoot.OuterXml }), false); } else { list2.Add(xmlNode.Name); } } FieldInfo fieldInfo = DirectXmlToObject.GetFieldInfoForType(t3.GetType(), xmlNode.Name, xmlRoot); if (fieldInfo == null) { foreach (FieldInfo fieldInfo2 in t3.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { foreach (object obj3 in fieldInfo2.GetCustomAttributes(typeof(LoadAliasAttribute), true)) { string alias = ((LoadAliasAttribute)obj3).alias; if (alias.EqualsIgnoreCase(xmlNode.Name)) { fieldInfo = fieldInfo2; break; } } if (fieldInfo != null) { break; } } } if (fieldInfo == null) { bool flag = false; foreach (object obj4 in t3.GetType().GetCustomAttributes(typeof(IgnoreSavedElementAttribute), true)) { string elementToIgnore = ((IgnoreSavedElementAttribute)obj4).elementToIgnore; if (string.Equals(elementToIgnore, xmlNode.Name, StringComparison.OrdinalIgnoreCase)) { flag = true; break; } } if (!flag) { Log.Error(string.Concat(new string[] { "XML error: ", xmlNode.OuterXml, " doesn't correspond to any field in type ", t3.GetType().Name, ". Context: ", xmlRoot.OuterXml }), false); } } else if (typeof(Def).IsAssignableFrom(fieldInfo.FieldType)) { if (xmlNode.InnerText.NullOrEmpty()) { fieldInfo.SetValue(t3, null); } else { DirectXmlCrossRefLoader.RegisterObjectWantsCrossRef(t3, fieldInfo, xmlNode.InnerText); } } else { object value = null; try { MethodInfo method3 = typeof(DirectXmlToObject).GetMethod("ObjectFromXml", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); MethodInfo methodInfo4 = method3.MakeGenericMethod(new Type[] { fieldInfo.FieldType }); value = methodInfo4.Invoke(null, new object[] { xmlNode, doPostLoad }); } catch (Exception ex3) { Log.Error("Exception loading from " + xmlNode.ToString() + ": " + ex3.ToString(), false); goto IL_863; } if (!typeof(T).IsValueType) { fieldInfo.SetValue(t3, value); } else { object obj5 = t3; fieldInfo.SetValue(obj5, value); t3 = (T)((object)obj5); } } } IL_863 :; } if (doPostLoad) { DirectXmlToObject.TryDoPostLoad(t3); } result = t3; } return(result); }
public static T ObjectFromXml <T>(XmlNode xmlRoot, bool doPostLoad) where T : new() { MethodInfo methodInfo = DirectXmlToObject.CustomDataLoadMethodOf(typeof(T)); if (methodInfo != null) { xmlRoot = XmlInheritance.GetResolvedNodeFor(xmlRoot); Type type = DirectXmlToObject.ClassTypeOf <T>(xmlRoot); T val = (T)Activator.CreateInstance(type); try { methodInfo.Invoke(val, new object[1] { xmlRoot }); } catch (Exception ex) { Log.Error("Exception in custom XML loader for " + typeof(T) + ". Node is:\n " + xmlRoot.OuterXml + "\n\nException is:\n " + ex.ToString()); val = default(T); } if (doPostLoad) { DirectXmlToObject.TryDoPostLoad(val); } return(val); } if (xmlRoot.ChildNodes.Count == 1 && xmlRoot.FirstChild.NodeType == XmlNodeType.CDATA) { if (typeof(T) != typeof(string)) { Log.Error("CDATA can only be used for strings. Bad xml: " + xmlRoot.OuterXml); return(default(T)); } return((T)(object)xmlRoot.FirstChild.Value); } if (xmlRoot.ChildNodes.Count == 1 && xmlRoot.FirstChild.NodeType == XmlNodeType.Text) { try { return((T)ParseHelper.FromString(xmlRoot.InnerText, typeof(T))); } catch (Exception ex2) { Log.Error("Exception parsing " + xmlRoot.OuterXml + " to type " + typeof(T) + ": " + ex2); } return(default(T)); } if (Attribute.IsDefined(typeof(T), typeof(FlagsAttribute))) { List <T> list = DirectXmlToObject.ListFromXml <T>(xmlRoot); int num = 0; foreach (T item in list) { int num2 = (int)(object)item; num |= num2; } return((T)(object)num); } if (typeof(T).HasGenericDefinition(typeof(List <>))) { MethodInfo method = typeof(DirectXmlToObject).GetMethod("ListFromXml", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); Type[] genericArguments = typeof(T).GetGenericArguments(); MethodInfo methodInfo2 = method.MakeGenericMethod(genericArguments); object[] parameters = new object[1] { xmlRoot }; object obj = methodInfo2.Invoke(null, parameters); return((T)obj); } if (typeof(T).HasGenericDefinition(typeof(Dictionary <, >))) { MethodInfo method2 = typeof(DirectXmlToObject).GetMethod("DictionaryFromXml", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); Type[] genericArguments2 = typeof(T).GetGenericArguments(); MethodInfo methodInfo3 = method2.MakeGenericMethod(genericArguments2); object[] parameters2 = new object[1] { xmlRoot }; object obj2 = methodInfo3.Invoke(null, parameters2); return((T)obj2); } if (!xmlRoot.HasChildNodes) { if (typeof(T) == typeof(string)) { return((T)(object)string.Empty); } XmlAttribute xmlAttribute = xmlRoot.Attributes["IsNull"]; if (xmlAttribute != null && xmlAttribute.Value.ToUpperInvariant() == "TRUE") { return(default(T)); } if (typeof(T).IsGenericType) { Type genericTypeDefinition = typeof(T).GetGenericTypeDefinition(); if (genericTypeDefinition != typeof(List <>) && genericTypeDefinition != typeof(HashSet <>) && genericTypeDefinition != typeof(Dictionary <, >)) { goto IL_03ed; } return(new T()); } } goto IL_03ed; IL_03ed: xmlRoot = XmlInheritance.GetResolvedNodeFor(xmlRoot); Type type2 = DirectXmlToObject.ClassTypeOf <T>(xmlRoot); T val2 = (T)Activator.CreateInstance(type2); List <string> list2 = null; if (xmlRoot.ChildNodes.Count > 1) { list2 = new List <string>(); } for (int i = 0; i < xmlRoot.ChildNodes.Count; i++) { XmlNode xmlNode = xmlRoot.ChildNodes[i]; if (!(xmlNode is XmlComment)) { if (xmlRoot.ChildNodes.Count > 1) { if (list2.Contains(xmlNode.Name)) { Log.Error("XML " + typeof(T) + " defines the same field twice: " + xmlNode.Name + ".\n\nField contents: " + xmlNode.InnerText + ".\n\nWhole XML:\n\n" + xmlRoot.OuterXml); } else { list2.Add(xmlNode.Name); } } FieldInfo fieldInfo = DirectXmlToObject.GetFieldInfoForType(val2.GetType(), xmlNode.Name, xmlRoot); if (fieldInfo == null) { FieldInfo[] fields = val2.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); int num3 = 0; while (num3 < fields.Length) { FieldInfo fieldInfo2 = fields[num3]; object[] customAttributes = fieldInfo2.GetCustomAttributes(typeof(LoadAliasAttribute), true); foreach (object obj3 in customAttributes) { string alias = ((LoadAliasAttribute)obj3).alias; if (alias.EqualsIgnoreCase(xmlNode.Name)) { fieldInfo = fieldInfo2; break; } } if (fieldInfo == null) { num3++; continue; } break; } } if (fieldInfo == null) { bool flag = false; object[] customAttributes2 = val2.GetType().GetCustomAttributes(typeof(IgnoreSavedElementAttribute), true); foreach (object obj4 in customAttributes2) { string elementToIgnore = ((IgnoreSavedElementAttribute)obj4).elementToIgnore; if (string.Equals(elementToIgnore, xmlNode.Name, StringComparison.OrdinalIgnoreCase)) { flag = true; break; } } if (!flag) { Log.Error("XML error: " + xmlNode.OuterXml + " doesn't correspond to any field in type " + val2.GetType().Name + "."); } } else if (typeof(Def).IsAssignableFrom(fieldInfo.FieldType)) { if (xmlNode.InnerText.NullOrEmpty()) { fieldInfo.SetValue(val2, null); } else { DirectXmlCrossRefLoader.RegisterObjectWantsCrossRef(val2, fieldInfo, xmlNode.InnerText); } } else { object obj5 = null; try { MethodInfo method3 = typeof(DirectXmlToObject).GetMethod("ObjectFromXml", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); MethodInfo methodInfo4 = method3.MakeGenericMethod(fieldInfo.FieldType); obj5 = methodInfo4.Invoke(null, new object[2] { xmlNode, doPostLoad }); } catch (Exception ex3) { Log.Error("Exception loading from " + xmlNode.ToString() + ": " + ex3.ToString()); continue; } if (!typeof(T).IsValueType) { fieldInfo.SetValue(val2, obj5); } else { object obj6 = val2; fieldInfo.SetValue(obj6, obj5); val2 = (T)obj6; } } } } if (doPostLoad) { DirectXmlToObject.TryDoPostLoad(val2); } return(val2); }