Пример #1
0
        internal protected void LoadFromXml(XmlNode subRootNode)
        {
            XmlAttribute xmlAttribute = subRootNode.Attributes["GUID"];

            if (xmlAttribute == null)
            {
                return;
            }
            if (!IdentifyGuid(xmlAttribute.Value))
            {
                return;
            }

            Dictionary <string, PropertyInfo> propSaveLoad = new System.Collections.Generic.Dictionary <String, PropertyInfo>();

            foreach (PropertyInfo prop in (this.GetType()).GetProperties())
            {
                foreach (object attribute in prop.GetCustomAttributes(true))
                {
                    if (attribute is SaveLoadAttribute)
                    {
                        if (string.IsNullOrEmpty(attribute.ToString()))
                        {
                            propSaveLoad.Add(prop.Name, prop);
                        }
                        else
                        {
                            propSaveLoad.Add(attribute.ToString(), prop);
                        }
                    }
                }
            }

            foreach (XmlNode node in subRootNode.ChildNodes)
            {
                String s = node.InnerText;
                if (propSaveLoad.ContainsKey(node.Name))
                {
                    PropertyInfo thisProp = propSaveLoad[node.Name];
                    if (thisProp != null)
                    {
                        string valueText = s;
                        if (SerializeData.Deserialize(thisProp.PropertyType, valueText, out object value))
                        {
                            if (value != null)
                            {
                                thisProp.SetValue(this, value, null);
                            }
                        }
                        else
                        {
                            if (value != null)
                            {
                                throw new Exception(value.ToString());
                            }
                            else
                            {
                                throw new Exception("in '" + thisProp.Name + "' ,Can not determine type ");
                            }
                        }
                    }
                    continue;
                }
                else
                {
                    InternalLoadFromXml(node);
                }
            }
        }