C# ConfigurationSection 的用法

操作代码

namespace Console
{
    public sealed class RedisConfig : ConfigurationSection
    {
        public static RedisConfig GetConfig()
        {
            RedisConfig section = GetConfig("RedisConfig");
            return section;
        }
        public static RedisConfig GetConfig(string sectionName)
        {
            RedisConfig section = (RedisConfig)ConfigurationManager.GetSection(sectionName);
            if (section == null)
                throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");
            return section;
        }
        /// 
        /// 可写的Redis链接地址
        /// 
        [ConfigurationProperty("WriteServerConStr", IsRequired = false)]
        public string WriteServerConStr
        {
            get
            {
                return (string)base["WriteServerConStr"];
            }
            set
            {
                base["WriteServerConStr"] = value;
            }
        }
        /// 
        /// 可读的Redis链接地址
        /// 
        [ConfigurationProperty("ReadServerConStr", IsRequired = false)]
        public string ReadServerConStr
        {
            get
            {
                return (string)base["ReadServerConStr"];
            }
            set
            {
                base["ReadServerConStr"] = value;
            }
        }
    }
}

配置文件


<configuration>
  <configSections>
      <section name="RedisConfig" type="Console.RedisConfig,Console"  />
  configSections>
  <RedisConfig   WriteServerConStr="192.168.1.62:6379" ReadServerConStr="192.168.1.62:6379">
  RedisConfig>
configuration>

你可能感兴趣的:(C#)