public static bool SaveConfig <T>(T t) where T : class, new() { ConfigFileAttribute attribute = AttributeHelper.GetConfigAttribute <ConfigFileAttribute>(typeof(T)); if (attribute == null) { ConfigThrowHelper.ThrowConfigException( R.ConfigError_NoConfigAttribute, typeof(T).FullName, typeof(ConfigFileAttribute).FullName); } ConfigSettings configSettings = FrameworkConfig.GetConfig <ConfigSettings>(); string configFilePath = string.Empty; switch (attribute.ConfigPathType) { case ConfigPathType.FullPhysicalPath: configFilePath = attribute.FileName; break; case ConfigPathType.ServerPath: configFilePath = HttpContext.Current.Server.MapPath(attribute.FileName); break; default: configFilePath = configSettings.GetConfigFilePath <T>(); break; } XmlConfigGetParameter getParameter = new XmlConfigGetParameter(configFilePath, attribute.IncludeSubdirectories); return(ObjectXmlSerializer.SaveXmlToFlie <T>(getParameter.FilePaths, t)); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static T GetConfig <T>() where T : ConfigurationSection, new() { DotNetConfigFileAttribute attribute = AttributeHelper.GetConfigAttribute <T, DotNetConfigFileAttribute>(); if (attribute == null) { ConfigThrowHelper.ThrowConfigException( R.ConfigError_NoConfigAttribute, typeof(T).FullName, typeof(DotNetConfigFileAttribute).FullName); } return(GetConfig <T>(attribute.SectionName)); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public string GetConfigFilePath <T>() where T : class, new() { ConfigFileAttribute configFileAttribute = AttributeHelper.GetConfigAttribute <T, ConfigFileAttribute>(); if (configFileAttribute == null) { ConfigThrowHelper.ThrowConfigException(R.ConfigError_NoConfigAttribute, typeof(T).FullName, typeof(ConfigFileAttribute).FullName); } string rootPath = GetConfigFileRootPath <T>(); return(string.Format("{0}/{1}", rootPath.TrimEnd('/', '\\'), configFileAttribute.FileName.TrimStart('/', '\\'))); }
/// <summary> /// /// </summary> /// <param name="name"></param> /// <returns></returns> public static IConfigSource Create(string name) { Check.Argument.IsNotNull("name", name); ConfigSettings configSettings = FrameworkConfig.GetConfig <ConfigSettings>(); ConfigProviderMapping providerMapping = configSettings.GetProviderMapping(name); IConfigSource configSource = MethodCaller.CreateInstance(providerMapping.Type) as IConfigSource; if (configSource == null) { ConfigThrowHelper.ThrowConfigException(R.ConfigSourceNotFound, name); } return(configSource); }
/// <summary> /// 用正在属性化的DotNet配置文件位置特性初始化 <see cref="ConfigFileSectionAttribute"/> 类的新实例。 /// </summary> /// <param name="sectionGroupType">DotNet配置结点组类型。</param> /// <param name="sectionName">DotNet配置结点名。。</param> public DotNetConfigFileAttribute(string fileName, string sectionName, Type sectionGroupType) : base(fileName) { this.sectionName = sectionName; this.FileName = fileName; DotNetConfigFileAttribute attribute = Attribute.GetCustomAttribute(sectionGroupType, typeof(DotNetConfigFileAttribute)) as DotNetConfigFileAttribute; if (attribute == null) { ConfigThrowHelper.ThrowConfigException( R.ConfigError_NoConfigAttribute, sectionGroupType.FullName, typeof(DotNetConfigFileAttribute).FullName); } this.sectionGroupType = sectionGroupType; }
/// <summary> /// 获取所有配置文件映射。 /// </summary> /// <value>所有配置文件映射。</value> /// <exception cref="ConfigurationErrorsException">配置文件映射关系名重复。</exception> /// <remarks> /// 一个配置文件只能从属于一个文件组。 /// </remarks> public FileMappingCollection GetFileMappings() { FileMappingCollection fileMappings = new FileMappingCollection(); if (FileGroupMappings != null) { foreach (FileGroupMapping group in FileGroupMappings) { foreach (FileMapping file in group.FileMappings) { if (fileMappings.Contains(file.Name)) { ConfigThrowHelper.ThrowConfigException(R.ConfigError_DuplicateFileMapping, file.Name); } file.FileGroupMapping = group; fileMappings.Add(file); } } } return(fileMappings); }