示例#1
0
        public static XElement GnosisBuildXML(object gnosisObject)
        {
            XElement element = new XElement(gnosisObject.GetType().Name);

            var propertyInfo = gnosisObject.GetType().GetProperties();

            //attributes
            var properties = propertyInfo.Where(prop => prop.IsDefined(typeof(GnosisPropertyAttribute), false));

            foreach (var property in properties)
            {
                string name = PropertyNameConverter.GetXMLPropertyName(property.Name);
                if (name == null)
                {
                    name = property.Name;
                }

                if (property.GetValue(gnosisObject) != null)
                {
                    XAttribute att = new XAttribute(name, property.GetValue(gnosisObject));
                    element.Add(att);
                }
            }

            //single childs
            var childs = propertyInfo.Where(prop => prop.IsDefined(typeof(GnosisChildAttribute), false));

            foreach (var child in childs)
            {
                XElement childElement = GnosisBuildXML(child);
                element.Add(childElement);
            }

            //lists
            var lists = propertyInfo.Where(prop => prop.IsDefined(typeof(GnosisCollectionAttribute), false));

            foreach (var list in lists)
            {
                if (list.PropertyType.GetGenericTypeDefinition() == typeof(List <>))
                {
                    var           items    = list.GetValue(gnosisObject);
                    List <object> itemList = (items as IEnumerable <object>).Cast <object>().ToList();
                    foreach (var item in itemList)
                    {
                        XElement childElement = GnosisBuildXML(item);
                        element.Add(childElement);
                    }
                }
            }

            return(element);
        }
示例#2
0
        public static object GnosisDeserializeXML(XElement baseElement)
        {
            string className  = "GnosisControls." + baseElement.Name.LocalName;
            var    obj        = Activator.CreateInstance(null, className);
            object baseObject = obj.Unwrap();

            //attributes
            foreach (var xAttribute in baseElement.Attributes())
            {
                string shivaName = PropertyNameConverter.GetShivaPropertyName(xAttribute.Name.ToString());
                if (shivaName == null)
                {
                    shivaName = xAttribute.Name.ToString();
                }

                PropertyInfo propInfo = baseObject.GetType().GetProperty(shivaName);
                if (propInfo == null)
                {
                    //    var props = baseObject.GetType().GetProperties();
                    GlobalData.Singleton.ErrorHandler.HandleError("Property not implemented on " + baseObject.GetType().Name + ": " + shivaName,
                                                                  "GnosisXMLHelper");
                }
                //propInfo.SetValue(baseObject, attribute.Value);
                else
                {
                    object val;
                    if (propInfo.PropertyType == typeof(bool) && xAttribute.Value.Equals("0"))
                    {
                        val = false;
                    }
                    else if (propInfo.PropertyType == typeof(bool) && xAttribute.Value.Equals("1"))
                    {
                        val = true;
                    }
                    else
                    {
                        val = Convert.ChangeType(xAttribute.Value, propInfo.PropertyType);
                    }

                    propInfo.SetValue(baseObject, val, null);
                }
            }

            //elements
            //content of GnosisInstance, GnosisDataCache is unknown so is not deserialized
            if (!(baseObject is GnosisInstance || baseObject is GnosisDataCache))
            {
                foreach (var element in baseElement.Elements())
                {
                    var child = GnosisDeserializeXML(element);
                    ((IGnosisObject)baseObject).GnosisAddChild((IGnosisObject)child);
                }
            }

            //if (baseObject is GnosisEntity)
            //{
            //    int i = 1;
            //}

            return(baseObject);
        }