Exemplo n.º 1
0
        /// <summary>
        /// XmlObject 将object序列化为xml字符串
        /// </summary>
        public static string XmlObject(object obj)
        {
            XmlDocument xmlDoc  = new XmlDocument();
            XmlElement  objNode = xmlDoc.CreateElement("object");

            SpringFox.SetPropertyValue(objNode, "type", TypeHelper.GetTypeRegularName(obj.GetType()), XmlPropertyPosition.Attribute);
            SpringFox.DoXmlObject(obj, xmlDoc, objNode);

            return(objNode.OuterXml);
        }
Exemplo n.º 2
0
        /// <summary>
        /// ObjectXml 将xml字符串转换为object
        /// </summary>
        public static object ObjectXml(string xml)
        {
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(xml);
            XmlNode objNode = xmlDoc.ChildNodes[0];
            Type    objType = ReflectionHelper.GetType(XmlHelper.GetPropertyValue(objNode, "type"));
            object  obj     = Activator.CreateInstance(objType);

            SpringFox.ConfigObject(objNode, ref obj);

            return(obj);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Load 将XML配置转换为Object
        /// </summary>
        public static AgileConfiguration Load(string configFilePath)
        {
            if (!File.Exists(configFilePath))
            {
                return(null);
            }

            XmlDocument doc = new XmlDocument();

            doc.Load(configFilePath);

            return((AgileConfiguration)SpringFox.ObjectXml(doc.ChildNodes[0].OuterXml));
        }
Exemplo n.º 4
0
        /// <summary>
        /// DoXmlObject 根据obj的各个属性创建对应的Node作为curNode的子节点
        /// </summary>
        private static void DoXmlObject(object obj, XmlDocument xmlDoc, XmlNode curNode)
        {
            try
            {
                if (TypeHelper.IsSimpleType(obj.GetType()))
                {
                    return;
                }

                #region logic
                foreach (PropertyInfo pro in obj.GetType().GetProperties())
                {
                    if ((!pro.CanRead))
                    {
                        continue;
                    }

                    object[] nonXmlAttrs = pro.GetCustomAttributes(typeof(NonXmlAttribute), true);
                    if ((nonXmlAttrs != null) && (nonXmlAttrs.Length > 0))
                    {
                        continue;
                    }

                    object proVal = pro.GetValue(obj, null);
                    if (proVal == null)
                    {
                        continue;
                    }

                    if (TypeHelper.IsSimpleType(pro.PropertyType))
                    {
                        SpringFox.SetPropertyValue(curNode, pro.Name, proVal.ToString(), XmlPropertyPosition.ChildNode);
                    }
                    else
                    {
                        bool isGenericList = pro.PropertyType.IsGenericType && (pro.PropertyType.GetGenericTypeDefinition() == typeof(IList <>));

                        if (isGenericList)
                        {
                            #region IList<>
                            XmlElement proNode = xmlDoc.CreateElement("property");
                            SpringFox.SetPropertyValue(proNode, "name", pro.Name, XmlPropertyPosition.Attribute);
                            curNode.AppendChild(proNode);

                            Type listElementType = pro.PropertyType.GetGenericArguments()[0];

                            XmlElement listNode = xmlDoc.CreateElement("list");
                            SpringFox.SetPropertyValue(listNode, "element-type", TypeHelper.GetTypeRegularName(listElementType), XmlPropertyPosition.Attribute);
                            proNode.AppendChild(listNode);

                            if (TypeHelper.IsSimpleType(listElementType))
                            {
                                foreach (object element in (IEnumerable)proVal)
                                {
                                    XmlHelper.SetPropertyValue(listNode, "value", element.ToString(), XmlPropertyPosition.ChildNode, false);
                                }
                            }
                            else
                            {
                                foreach (object element in (IEnumerable)proVal)
                                {
                                    XmlElement elementNode = xmlDoc.CreateElement("object");
                                    if (listElementType != element.GetType())
                                    {
                                        XmlHelper.SetPropertyValue(elementNode, "type", TypeHelper.GetTypeRegularName(element.GetType()), XmlPropertyPosition.Attribute);
                                    }
                                    listNode.AppendChild(elementNode);
                                    SpringFox.DoXmlObject(element, xmlDoc, elementNode);
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            #region 非集合、非数组
                            bool isCollection = typeof(IEnumerable).IsAssignableFrom(pro.PropertyType);
                            bool isAry        = pro.PropertyType.IsSubclassOf(typeof(Array));

                            if ((!isCollection) && (!isAry))
                            {
                                XmlElement proNode = xmlDoc.CreateElement("property");
                                SpringFox.SetPropertyValue(proNode, "name", pro.Name, XmlPropertyPosition.Attribute);
                                curNode.AppendChild(proNode);

                                XmlElement subProNode = xmlDoc.CreateElement("object");
                                SpringFox.SetPropertyValue(subProNode, "type", TypeHelper.GetTypeRegularName(proVal.GetType()), XmlPropertyPosition.Attribute);
                                proNode.AppendChild(subProNode);

                                SpringFox.DoXmlObject(proVal, xmlDoc, subProNode);
                            }
                            #endregion
                        }
                    }
                }
                #endregion
            }
            catch (Exception ee)
            {
                string msg = string.Format("XmlObject {0} Error !", obj.GetType().ToString());
                throw new Exception(msg, ee);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// ConfigObject 使用objNode的各个子节点配置target的对应的属性
        /// </summary>
        private static void ConfigObject(XmlNode objNode, ref object target)
        {
            foreach (XmlAttribute attr in objNode.Attributes)
            {
                ReflectionHelper.SetProperty(target, attr.Name, attr.Value);
            }

            foreach (XmlNode childNode in objNode.ChildNodes)
            {
                if (childNode.Attributes["value"] != null)
                {
                    ReflectionHelper.SetProperty(target, childNode.Attributes["name"].Value, childNode.Attributes["value"].Value);
                }
                else
                {
                    XmlNode childProNode = childNode.ChildNodes[0];
                    if (childProNode.Name == "object")
                    {
                        Type   proType = ReflectionHelper.GetType(childProNode.Attributes["type"].Value);
                        object proObj  = Activator.CreateInstance(proType);
                        SpringFox.ConfigObject(childProNode, ref proObj);
                        ReflectionHelper.SetProperty(target, childNode.Attributes["name"].Value, proObj);
                    }
                    else if (childProNode.Name == "list")
                    {
                        Type   listElementType       = ReflectionHelper.GetType(childProNode.Attributes["element-type"].Value);
                        Type   closedGenericListType = typeof(List <>).MakeGenericType(listElementType);
                        object list = Activator.CreateInstance(closedGenericListType);
                        //ISimpleList simpleList = (ISimpleList)SpringFox.DynamicProxyCreator.CreateDynamicProxy<ISimpleList>(list);

                        #region Add object into list
                        if (TypeHelper.IsSimpleType(listElementType))
                        {
                            foreach (XmlNode elementNode in childProNode.ChildNodes)
                            {
                                object element = TypeHelper.ChangeType(listElementType, elementNode.InnerText);
                                closedGenericListType.GetMethod("Add").Invoke(list, new object[] { element });
                                //simpleList.Add(element);
                            }
                        }
                        else
                        {
                            foreach (XmlNode elementNode in childProNode.ChildNodes)
                            {
                                Type curElementType = listElementType;
                                if (elementNode.Attributes["type"] != null)
                                {
                                    curElementType = ReflectionHelper.GetType(elementNode.Attributes["type"].Value);
                                }
                                object element = Activator.CreateInstance(curElementType);
                                SpringFox.ConfigObject(elementNode, ref element);
                                closedGenericListType.GetMethod("Add").Invoke(list, new object[] { element });
                                //simpleList.Add(element);
                            }
                        }

                        #endregion

                        ReflectionHelper.SetProperty(target, childNode.Attributes["name"].Value, list);
                    }
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Save 将配置保存到xml文件
        /// </summary>
        public void Save(string configFilePath)
        {
            string xml = SpringFox.XmlObject(this);

            FileHelper.GenerateFile(configFilePath, xml);
        }