Esempio n. 1
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);
        }
Esempio n. 2
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);
                    }
                }
            }
        }