private static Instance Reflect_XML(XmlNode node, Type objType) { if (!typeof(Instance).IsAssignableFrom(objType)) { throw new Exception("'T' must be an Instance."); } Instance obj = (Instance)Activator.CreateInstance(objType); List <XmlNode> children = new List <XmlNode>(); XmlNode properties = null; foreach (XmlNode child in node.ChildNodes) { if (child.Name == "Properties") { properties = child; } else if (child.Name == "Item") { children.Add(child); } } if (properties != null) { foreach (XmlNode property in properties.ChildNodes) { string propertyName = property.Attributes.GetNamedItem("name").Value; propertyName = propertyName.Replace(" ", "_"); FieldInfo field = objType.GetField(propertyName, fieldInfoFlags); if (field != null && property.FirstChild != null) { string propertyType = property.Name; object value = null; string sValue = property.FirstChild.Value; if (propertyType == "string") { value = sValue; } else if (propertyType == "float") { value = Format.ParseFloat(sValue); } else if (propertyType == "double") { value = Format.ParseDouble(sValue); } else if (propertyType == "int") { value = Format.ParseInt(sValue); } else if (propertyType == "CoordinateFrame") { value = CFrame.FromXml(property); } else if (propertyType == "Vector3") { value = new Vector3(property); } else if (propertyType == "Content") { value = property.InnerText; } if (propertyType == "token") { Type fieldType = field.FieldType; if (fieldType.IsEnum) { int index = int.Parse(sValue); value = Enum.ToObject(fieldType, index); } } else if (propertyType == "Color3uint8") { uint rgb = uint.Parse(sValue); int r = (int)(rgb / 65536) % 256; int g = (int)(rgb / 256) % 256; int b = (int)(rgb % 256); value = Color.FromArgb(r, g, b); } if (value != null) { if (field.FieldType == typeof(BrickColor)) { int brickColorId = (int)value; value = BrickColor.FromNumber(brickColorId); } field.SetValue(obj, value); } } } } foreach (XmlNode child in children) { XmlNode classNameNode = child.Attributes.GetNamedItem("class"); if (classNameNode != null) { Type classType = GetClassType(classNameNode.Value); if (classType != null) { Instance childObj = Reflect_XML(child, classType); childObj.Parent = obj; } } } return(obj); }
private static Instance Reflect_XML(XmlNode node, Type objType) { if (!typeof(Instance).IsAssignableFrom(objType)) { throw new Exception("'T' must be an Instance."); } Instance obj = (Instance)Activator.CreateInstance(objType); List <XmlNode> children = new List <XmlNode>(); XmlNode properties = null; foreach (XmlNode child in node.ChildNodes) { if (child.Name == "Properties") { properties = child; } else if (child.Name == "Item") { children.Add(child); } } if (properties != null) { foreach (XmlNode property in properties.ChildNodes) { string propertyName = property.Attributes.GetNamedItem("name").Value; propertyName = propertyName.Replace(" ", "_"); FieldInfo field = objType.GetField(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy); if (field != null) { string propertyType = property.Name; object value = null; string sValue = property.FirstChild.Value; if (propertyType == "string") { value = sValue; } else if (propertyType == "float") { value = float.Parse(sValue, Rbx2Source.NormalParse); } else if (propertyType == "double") { value = double.Parse(sValue, Rbx2Source.NormalParse); } else if (propertyType == "int") { value = int.Parse(sValue, Rbx2Source.NormalParse); } else if (propertyType == "CoordinateFrame") { value = CFrame.FromXml(property); } else if (propertyType == "Vector3") { value = new Vector3(property); } else if (propertyType == "Content") { if (property.FirstChild != null && property.FirstChild.FirstChild != null) { value = property.FirstChild.FirstChild.Value; } } else if (propertyType == "token") { Type fieldType = field.FieldType; if (fieldType.IsEnum) { int index = int.Parse(sValue); value = Enum.ToObject(fieldType, index); } } if (value != null) { field.SetValue(obj, value); } } } } foreach (XmlNode child in children) { XmlNode classNameNode = child.Attributes.GetNamedItem("class"); if (classNameNode != null) { Type classType = GetClassType(classNameNode.Value); if (classType != null) { Instance childObj = Reflect_XML(child, classType); childObj.Parent = obj; } } } return(obj); }