/// <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); }
internal static ConfigRootAttribute GetRootConfigRootAttribute(Type configType, ConfigAttributeTypes configAttributeTypes) { ConfigRootAttribute configAttribute; Attribute attri = configType.GetCustomAttribute(configAttributeTypes.RootAttributeType, false); if (attri == null) { //未标记特性,创建默认值 configAttribute = new ConfigRootAttribute(configType.Name, null, true); } else { configAttribute = (ConfigRootAttribute)attri; } return(configAttribute); }
/// <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); }