]> O.S.I.I.S - jp/crow.git/commitdiff
load goml with loop on xml attributes, not eventInfos and
authorjpbruyere <jp.bruyere@hotmail.com>
Sun, 13 Sep 2015 13:03:36 +0000 (15:03 +0200)
committerjpbruyere <jp.bruyere@hotmail.com>
Sun, 13 Sep 2015 13:03:36 +0000 (15:03 +0200)
PropertyInfos, but it will not allow anymore to use custom name for
xmlAttribute, they will always have to be the same as in c#

src/GraphicObjects/GraphicObject.cs

index b92d666ce187bb97d4d7b71e39c3a50765e9050f..98b52b0d45813f46a824a4b5ce3131074ad3050c 100644 (file)
@@ -736,82 +736,52 @@ namespace go
                {\r
                        return null;\r
                }\r
-               //TODO: allert or inform when unknow attribute name in XML, very confusing...\r
                public virtual void ReadXml (System.Xml.XmlReader reader)\r
                {\r
-                       foreach (EventInfo ei in this.GetType().GetEvents()) {\r
-//                             FieldInfo fi = this.GetType().GetField(ei.Name,\r
-//                                     BindingFlags.NonPublic |\r
-//                                     BindingFlags.Instance |\r
-//                                     BindingFlags.GetField);\r
-                               string handler = reader.GetAttribute(ei.Name);\r
-                               if (string.IsNullOrEmpty (handler))\r
+                       if (!reader.HasAttributes)\r
+                               return;\r
+                       Type thisType = this.GetType ();\r
+                       while (reader.MoveToNextAttribute ()) {\r
+                               string attName = reader.Name;\r
+                               string attValue = reader.Value;\r
+                               MemberInfo mi = thisType.GetMember (attName).FirstOrDefault();\r
+                               if (mi == null) {\r
+                                       Debug.WriteLine (Interface.CurrentGOMLPath + "=>GOML: Unknown attribute in " + thisType.ToString() + " : " + attName);\r
                                        continue;\r
-                                       \r
-                               Interface.GOMLResolver.Add(new DynAttribute \r
-                                       { \r
+                               }\r
+                               if (mi.MemberType == MemberTypes.Event) {\r
+                                       Interface.GOMLResolver.Add (new DynAttribute \r
                                                Source = this, \r
-                                               Value = handler,\r
-                                               MemberName = ei.Name\r
+                                               Value = attValue,\r
+                                               MemberName = attName\r
                                        });\r
-                       }\r
-                       foreach (PropertyInfo pi in this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) {\r
-                               if (pi.GetSetMethod () == null)\r
-                                       continue;\r
-\r
-                               bool isAttribute = false;\r
-\r
-                               string name = "";\r
-                               object value = null;\r
-                               object defaultValue = null;\r
+                               } else if (mi.MemberType == MemberTypes.Property) {\r
+                                       PropertyInfo pi = mi as PropertyInfo;\r
 \r
-                               Type valueType = null;\r
-\r
-\r
-                               MemberInfo mi = pi.GetGetMethod ();\r
-\r
-                               //if (mi.MemberType == MemberTypes.Property)\r
-                               //{\r
-                               //    PropertyInfo pi = mi as PropertyInfo;\r
-                               //    value = pi.GetValue(this, null);\r
-                               //    valueType = pi.PropertyType;\r
-                               //}\r
-                               //else if (mi.MemberType == MemberTypes.Field)\r
-                               //{\r
-                               //    FieldInfo fi = mi as FieldInfo;\r
-                               //    value = fi.GetValue(this);\r
-                               //    valueType = fi.FieldType;\r
-                               //}\r
-\r
-                               object[] att = pi.GetCustomAttributes (false);\r
-\r
-                               foreach (object o in att) {\r
-                                       XmlAttributeAttribute xaa = o as XmlAttributeAttribute;\r
-                                       if (xaa != null) {\r
-                                               isAttribute = true;\r
-                                               if (string.IsNullOrEmpty (xaa.AttributeName))\r
-                                                       name = pi.Name;\r
-                                               else\r
-                                                       name = xaa.AttributeName;\r
+                                       if (pi.GetSetMethod () == null)\r
                                                continue;\r
-                                       }\r
 \r
-                                       XmlIgnoreAttribute xia = o as XmlIgnoreAttribute;\r
-                                       if (xia != null)\r
-                                               continue;\r
+                                       bool isAttribute = false;\r
+                                       object defaultValue = null;\r
 \r
-                                       DefaultValueAttribute dv = o as DefaultValueAttribute;\r
-                                       if (dv != null) {\r
-                                               defaultValue = dv.Value;\r
-                                               continue;\r
+                                       foreach (object o in pi.GetCustomAttributes ()) {\r
+                                               XmlAttributeAttribute xaa = o as XmlAttributeAttribute;\r
+                                               if (xaa != null) {\r
+                                                       isAttribute = true;\r
+                                                       if (!string.IsNullOrEmpty (xaa.AttributeName))\r
+                                                               attName = xaa.AttributeName;\r
+                                                       continue;\r
+                                               }\r
+                                               if (o is XmlIgnoreAttribute)\r
+                                                       break;\r
+                                               DefaultValueAttribute dv = o as DefaultValueAttribute;\r
+                                               if (dv != null)\r
+                                                       defaultValue = dv.Value;                                                \r
                                        }\r
-                               }\r
-\r
-\r
-                               if (isAttribute) {\r
-                                       string v = reader.GetAttribute (name);\r
-\r
-                                       if (string.IsNullOrEmpty (v)) {\r
+                                       if (!isAttribute)\r
+                                               continue;\r
+                                       \r
+                                       if (string.IsNullOrEmpty (attValue)) {\r
                                                //avoid system types automaticaly converted by parser\r
                                                if (defaultValue != null && !pi.PropertyType.Namespace.StartsWith("System")) {\r
                                                        if (pi.PropertyType != defaultValue.GetType()) {\r
@@ -822,50 +792,42 @@ namespace go
                                                                }\r
                                                        }\r
                                                }\r
-\r
                                                pi.SetValue (this, defaultValue, null);\r
-\r
                                        } else {\r
-                                               \r
-                                               if (v.StartsWith("{")) {\r
+\r
+                                               if (attValue.StartsWith("{")) {\r
                                                        //binding\r
-                                                       if (!v.EndsWith("}"))\r
-                                                               throw new Exception (string.Format("GOML:Malformed binding: {0}", v));\r
-                                                       \r
-                                                       string strBinding = v.Substring (1, v.Length - 2);\r
+                                                       if (!attValue.EndsWith("}"))\r
+                                                               throw new Exception (string.Format("GOML:Malformed binding: {0}", attValue));\r
+\r
+                                                       string strBinding = attValue.Substring (1, attValue.Length - 2);\r
                                                        Interface.GOMLResolver.Add (new DynAttribute () {\r
                                                                Source = this,\r
-                                                               MemberName = name,\r
+                                                               MemberName = attName,\r
                                                                Value = strBinding\r
                                                        });\r
                                                        continue;\r
                                                }\r
 \r
                                                if (pi.PropertyType == typeof(string)) {\r
-                                                       pi.SetValue (this, v, null);\r
+                                                       pi.SetValue (this, attValue, null);\r
                                                        continue;\r
                                                }\r
 \r
                                                object o = null;\r
 \r
                                                if (pi.PropertyType.IsEnum) {\r
-                                                       o = Enum.Parse (pi.PropertyType, v);\r
+                                                       o = Enum.Parse (pi.PropertyType, attValue);\r
                                                } else {\r
                                                        MethodInfo me = pi.PropertyType.GetMethod ("Parse", new Type[] { typeof(string) });\r
-                                                       o = me.Invoke (null, new string[] { v });\r
+                                                       o = me.Invoke (null, new string[] { attValue });\r
                                                }\r
 \r
                                                pi.SetValue (this, o, null);\r
-                                       }\r
-                               } else {\r
-                                       ////if member type is not serializable, cancel\r
-                                       //if (valueType.GetInterface("IXmlSerializable") == null)\r
-                                       //    continue;\r
-\r
-                                       //(pi.GetValue(this, null) as IXmlSerializable).WriteXml(writer);\r
+                                       }                                       \r
                                }\r
                        }\r
-\r
+                       reader.MoveToElement();\r
                }\r
                public virtual void WriteXml (System.Xml.XmlWriter writer)\r
                {\r