Пример #1
0
        /// <summary>
        /// 根据配置路径和段落名称获取配置对象
        /// </summary>
        /// <typeparam name="TConfig"></typeparam>
        /// <param name="configurationPath"></param>
        /// <param name="type">区分平台和插件</param>
        /// <param name="sectionName"></param>
        /// <returns></returns>
        private TConfig Get <TConfig>(string configurationPath, ConfigurationType type, string sectionName) where TConfig : class
        {
            string            key = configurationPath.ToLower();
            TConfig           tconfig;
            ConfigurationItem ci;

            if (configurationItemCache.ContainsKey(key))
            {
                ci = configurationItemCache[key];
            }
            else
            {
                ci = new ConfigurationItem()
                {
                    ConfigurationFilePath = configurationPath,
                    Type = type.ToDescription()
                };
                configurationItemCache.Add(key, ci);
            }
            if (sectionName != "")
            {
                tconfig = ci.Configuration.GetSection(sectionName) as TConfig;
                if (tconfig == null)
                {
                    throw new ConfigurationErrorsException("The configuration file {0} Lack of configuration section {1}".FormatString(ci.ConfigurationFilePath, sectionName));
                }
            }
            else
            {
                tconfig = ci.Configuration as TConfig;
            }
            return(tconfig);
        }
Пример #2
0
        /// <summary>
        /// 根据配置路径注册配置对象
        /// </summary>
        /// <param name="configurationPath">配置文件全路径</param>
        /// <param name="configType">区分平台和插件</param>
        /// <returns>是否注册成功</returns>
        public bool RegisterConfigurationObject(string configurationPath, ConfigurationType configType)
        {
            string            key = configurationPath.ToLower();
            ConfigurationItem ci  = new ConfigurationItem()
            {
                ConfigurationFilePath = configurationPath,
                Type = configType.ToDescription()
            };

            if (!configurationItemCache.ContainsKey(key))
            {
                configurationItemCache.Add(key, ci);
                return(false);
            }
            else
            {
                return(false);
            }
        }