コード例 #1
0
        /// <summary>
        /// 按节点名称从配置信息中取得节点,并将节点信息缓存,建立文件依赖
        /// </summary>
        /// <param name="sectionName">节点名称</param>
        /// <param name="checkNullSection">如果返回null,是否抛出异常</param>
        /// <returns>配置节点</returns>
        /// <remarks>
        /// 按名称获取配置节点信息。返回ConfigurationSection的派生类实体,实体类需由用户自定义。
        /// <code source="..\Framework\TestProjects\Deluxeworks.Library.WebTest\Configuration\Default.aspx.cs" region="Using Broker" lang="cs" title="使用配置管理" />
        /// </remarks>
        public static ConfigurationSection GetSection(string sectionName, bool checkNullSection = false)
        {
            ConfigurationSection section;

            if (false == ConfigurationSectionCache.Instance.TryGetValue(sectionName, out section))
            {
                ConfigFilesSetting settings = LoadFilesSetting();

                System.Configuration.Configuration globalConfig = null;
                System.Configuration.Configuration config       = GetFinalConfiguration(settings, out globalConfig);

                section = config.GetSectionRecursively(sectionName);

                List <string> dependentFiles = new List <string>();

                dependentFiles.Add(settings.MachineConfigurationFile);
                dependentFiles.Add(settings.LocalConfigurationFile);
                dependentFiles.Add(settings.MetaConfigurationFile);
                dependentFiles.Add(settings.GlobalConfigurationFile);

                string configSourceFile = globalConfig.GetSectionRelativeFile(section);

                if (configSourceFile.IsNotEmpty())
                {
                    dependentFiles.Add(configSourceFile);
                }

                FileCacheDependency dependency = new FileCacheDependency(true, dependentFiles.ToArray());

                ConfigurationSectionCache.Instance.Add(sectionName, section, dependency);
            }

            if (checkNullSection)
            {
                section.CheckSectionNotNull(sectionName);
            }

            return(section);
        }