关于config文件的自定义节点

1.设置定义的section节点必须继承ConfigurationSection类,并设置相关特性ConfigurationProperty,设置特性后,将设置的特性在app.config中进行填写;

2.单节点处理,不需要进行复杂的节点处理,直接配置,直接使用。

  1 public class RedisConfigInfo : ConfigurationSection
  2 {
  3         private static RedisConfigInfo setting;
  4         public static RedisConfigInfo Setting
  5         {
  6             get
  7             {
  8                 if (setting == null)
  9                 {
 10                     setting = (RedisConfigInfo)ConfigurationManager.GetSection("RedisConf");
 11                 }
 12                 return setting;
 13             }
 14         }
 15 
 16         [ConfigurationProperty("Host", IsRequired = true)]
 17         public string Host
 18         {
 19             set
 20             {
 21                 this["Host"] = value;
 22             }
 23             get
 24             {
 25                 return (string)this["Host"];
 26             }
 27         }
 28 
 29         [ConfigurationProperty("Port")]
 30         public int Port
 31         {
 32             get
 33             {
 34                 return (int)(this["Port"] ?? 6379);
 35             }
 36             set
 37             {
 38                 this["Port"] = value;
 39             }
 40         }
 41 
 42         [ConfigurationProperty("DaultDb")]
 43         public int DaultDb
 44         {
 45             set
 46             {
 47                 this["DaultDb"] = value;
 48             }
 49             get
 50             {
 51                 return (int)(this["DaultDb"] ?? 0);
 52             }
 53         }
 54 
 55         [ConfigurationProperty("MaxWritePoolSize")]
 56         public int MaxWritePoolSize
 57         {
 58             set
 59             {
 60                 this["MaxWritePoolSize"] = value;
 61             }
 62             get
 63             {
 64                 return (int)this["MaxWritePoolSize"];
 65             }
 66         }
 67 
 68         [ConfigurationProperty("MaxReadPoolSize")]
 69         public int MaxReadPoolSize
 70         {
 71             set
 72             {
 73                 this["MaxReadPoolSize"] = value;
 74             }
 75             get
 76             {
 77                 return (int)this["MaxReadPoolSize"];
 78             }
 79         }
 80 
 81         [ConfigurationProperty("AutoStart")]
 82         public bool AutoStart
 83         {
 84             set
 85             {
 86                 this["AutoStart"] = value;
 87             }
 88             get
 89             {
 90                 return (bool)this["AutoStart"];
 91             }
 92         }
 93 
 94         [ConfigurationProperty("WriteServerList")]
 95         public string WriteServerList
 96         {
 97             set
 98             {
 99                 this["WriteServerList"] = value;
100             }
101             get
102             {
103                 return (string)this["WriteServerList"];
104             }
105         }
106 
107         [ConfigurationProperty("ReadServerList")]
108         public string ReadServerList
109         {
110             set
111             {
112                 this["ReadServerList"] = value;
113             }
114             get
115             {
116                 return (string)this["ReadServerList"];
117             }
118      }
119 }

完成配置后,在对应文件中插入相关的配置:

1.声明引用:

 <configSections>
    <section name="RedisConf" type="Arrowtip.Common.RedisConf.RedisConfigInfo,Arrowtip.Common"/>
  </configSections>

2.细致使用针对section节点的配置:

<RedisConf Host="192.168.121.128" Port="6379" DaultDb="15" WriteServerList="192.168.121.128"
             ReadServerList="192.168.121.128" MaxWritePoolSize="1000" MaxReadPoolSize="1000" AutoStart="true">
</RedisConf>

以上是单节点配置,只能配置一个节点。

posted @ 2015-10-18 00:19  地下的知了  Views(253)  Comments(0Edit  收藏  举报