Esempio n. 1
0
        /// <summary>
        /// 从xml配置中读取配置到指定的配置对象中
        /// </summary>
        /// <param name="xdoc">xml配置</param>
        /// <param name="config">指定的配置对象</param>
        public void ReadConfigFromXDocument(XDocument xdoc, ref object config)
        {
            ConfigAttributeTypes configAttributeTypes = new ConfigAttributeTypes();
            Type configType = config.GetType();
            ConfigRootAttribute configRootAttribute = ConfigRootAttribute.GetRootConfigRootAttribute(configType, configAttributeTypes);
            XElement            rootEle             = xdoc.XPathSelectElement(configRootAttribute.GetName(configType));

            if (rootEle == null)
            {
                return;
            }

            PropertyInfo[] propertyInfoArr = this.GetTypePropertyInfos(configType);
            this.ReadConfigToXml(rootEle, propertyInfoArr, ref config, configAttributeTypes);
        }
Esempio n. 2
0
        /// <summary>
        /// 写配置到XDocument
        /// </summary>
        /// <param name="config">配置对象</param>
        /// <returns>配置XDocument</returns>
        public XDocument WriteConfigToXDocument(object config)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            ConfigAttributeTypes configAttributeTypes = new ConfigAttributeTypes();
            XDocument            xdoc = new XDocument();
            Type configType           = config.GetType();
            ConfigRootAttribute configRootAttribute = ConfigRootAttribute.GetRootConfigRootAttribute(configType, configAttributeTypes);
            XElement            rootEle             = new XElement(configRootAttribute.GetName(configType));

            this.AddDes(rootEle, configRootAttribute.Des);

            PropertyInfo[] propertyInfoArr = this.GetTypePropertyInfos(configType);
            this.WriteConfigToXml(rootEle, propertyInfoArr, config, configAttributeTypes);
            xdoc.Add(rootEle);
            return(xdoc);
        }