除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果。

首先说下.Net配置文件中一个潜规则:

在配置节点时,对于想要进行存储的参数数据,可以采用两种方式:一种是存储到节点的属性中,另一种是存储在节点的文本中。

因为一个节点可以有很多属性,但是只要一个innertext,而要在程序中将这两种形式区分开会带来复杂性。 为了避免这个问题,.net的配置文件只是用属性存储而不使用innertext.

接着,我们来写一个符合这个潜规则的自定义配置文件,方便测试:

1
2
3
4
5
6
7
8
<mailservergroup provider="www.baidu.com">
    <mailservers>
      <mailserver address="13232@qq.com" client="http://blog.csdn.net/lhc1105" password="2343254" username="lhc">
      <mailserver address="132wdfgdsggtaewg32@qq.com" client="http://blog345.csdn.net/lhc1105" password="2334t243的萨芬234254" username="dfshs水田如雅">
      <mailserver address="132wdgadsfgdtaewg32@qq.com" client="http://blog436.csdn.net/lhc1105" password="23ewrty2343的萨芬234254" username="sdfhdfs水田如雅">
      <mailserver address="132wdgdfagdstaewg32@qq.com" client="http://blo345734g.csdn.net/lhc1105" password="23erwt43的萨芬234254" username="sdfher水田如雅">
    </mailserver></mailserver></mailserver></mailserver></mailservers>
  </mailservergroup>

接着,我们来写相应的处理类,这里我们由内向外来写:

首先是最内层的mailServer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/// <summary>
   /// Class MailServerElement:用于映射mailServer节点,这里是实际存储数据的地方;
   /// </summary>
   /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 21:51:57</remarks>
   public sealed class MailServerElement : ConfigurationElement  //配置文件中的配置元素
   {
 
       /// <summary>
       /// Gets or sets the client.
       /// </summary>
       /// <value>The client.</value>
       /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 22:05:40</remarks>
       [ConfigurationProperty("client", IsKey = true, IsRequired = true)]  //client是必须的key属性,有点儿主键的意思,例如,如果定义多个client相同的节点,循环读取的话就只读取到最后一个值
       public string Client
       {
           get
           {
               return this["client"] as string;
           }
           set
           {
               this["client"] = value;
           }
 
       }
       /// <summary>
       /// Gets or sets the address.
       /// </summary>
       /// <value>The address.</value>
       /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 22:05:38</remarks>
       [ConfigurationProperty("address")]
       public string Address
       {
           get
           {
               return this["address"] as string;
           }
           set
           {
               this["address"] = value;
           }
 
       }
       /// <summary>
       /// Gets or sets the name of the user.
       /// </summary>
       /// <value>The name of the user.</value>
       /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 22:05:35</remarks>
       [ConfigurationProperty("userName")]
       public string UserName
       {
 
           get
           {
               return this["userName"] as string;
           }
           set
           {
               this["userName"] = value;
           }
 
       }
       /// <summary>
       /// Gets or sets the password.
       /// </summary>
       /// <value>The password.</value>
       /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 22:05:33</remarks>
       [ConfigurationProperty("password")]
       public string Password
       {
 
           get
           {
               return this["password"] as string;
           }
           set
           {
               this["password"] = value;
           }
 
       }
 
 
 
   }

接着是mailServers,它是一个mailServer的集合:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/// <summary>
   /// Class MailServerCollection:映射mailServers节点,为一个集合类,另外还包含了很多对节点的操作方法,大部分继承自ConfigurationElementCollection
   /// </summary>
   /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 21:52:00</remarks>
   public sealed class MailServerCollection : ConfigurationElementCollection
   {
       /// <summary>
       /// 获取 <see cref="T:System.Configuration.ConfigurationElementCollection"> 的类型。
       /// </see></summary>
       /// <value>The type of the collection.</value>
       /// <returns>此集合的 <see cref="T:System.Configuration.ConfigurationElementCollectionType">。</see></returns>
       /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 22:05:08</remarks>
       public override ConfigurationElementCollectionType CollectionType
       {
           get
           {
               return ConfigurationElementCollectionType.BasicMap;
           }
         
       }
 
 
       /// <summary>
       /// 当在派生的类中重写时,创建一个新的 <see cref="T:System.Configuration.ConfigurationElement">。
       /// </see></summary>
       /// <returns>新的 <see cref="T:System.Configuration.ConfigurationElement">。</see></returns>
       /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 22:05:03</remarks>
       protected override ConfigurationElement CreateNewElement()
       {
           return new MailServerElement();
       }
 
       /// <summary>
       /// 在派生类中重写时获取指定配置元素的元素键。
       /// </summary>
       ///<param name="element">要为其返回键的 <see cref="T:System.Configuration.ConfigurationElement">。
       /// <returns>一个 <see cref="T:System.Object">,用作指定 <see cref="T:System.Configuration.ConfigurationElement"> 的键。</see></see></returns>
       /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 22:04:51</remarks>
       protected override object GetElementKey(ConfigurationElement element)
       {
           return (element as MailServerElement).Client;
       }
 
       /// <summary>
       /// 获取在派生的类中重写时用于标识配置文件中此元素集合的名称。
       /// </summary>
       /// <value>The name of the element.</value>
       /// <returns>集合的名称;否则为空字符串。默认值为空字符串。</returns>
       /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 23:41:40</remarks>
       protected override string ElementName
       {
           get
           {
               return "mailServer";
           }
       }
 
 
       /// <summary>
       /// 获取集合中的元素数。
       /// </summary>
       /// <value>The count.</value>
       /// <returns>集合中的元素数。</returns>
       /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 22:08:24</remarks>
       public new int Count
       {
           get { return base.Count; }
       }
 
       /// <summary>
       /// 获取或设置此配置元素的属性、特性或子元素。
       /// </summary>
       ///<param name="index">The index.
       /// <returns>MailServerElement.</returns>
       /// <remarks>Editor:v-liuhch</remarks>
       public MailServerElement this[int index]
       {
 
           get { return BaseGet(index) as MailServerElement; }
           set
           {
               if (BaseGet(index) != null)
               {
                   BaseRemoveAt(index);
               }
               BaseAdd(index, value);
           }
 
       }
 
       /// <summary>
       /// 获取或设置此配置元素的属性、特性或子元素。
       /// </summary>
       ///<param name="Name">The name.
       /// <returns>MailServerElement.</returns>
       /// <remarks>Editor:v-liuhch</remarks>
       new public MailServerElement this[string Name]
       {
           get { return BaseGet(Name) as MailServerElement; }
       }
 
       /// <summary>
       /// Indexes the of.
       /// </summary>
       ///<param name="element">The element.
       /// <returns>System.Int32.</returns>
       /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 22:24:16</remarks>
       public int IndexOf(MailServerElement element)
       {
 
           return BaseIndexOf(element);
       }
 
       /// <summary>
       /// Adds the specified element.
       /// </summary>
       ///<param name="element">The element.
       /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 22:26:06</remarks>
       public void Add(MailServerElement element)
       {
           BaseAdd(element);
       }
 
       /// <summary>
       /// Removes the specified element.
       /// </summary>
       ///<param name="element">The element.
       /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 22:27:01</remarks>
       public void Remove(MailServerElement element)
       {
           if (BaseIndexOf(element) > 0)
           {
               BaseRemove(element.Client);
           }
       }
 
       /// <summary>
       /// Removes at.
       /// </summary>
       ///<param name="index">The index.
       /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 22:33:29</remarks>
       public void RemoveAt(int index)
       {
           BaseRemoveAt(index);
       }
 
       /// <summary>
       /// Removes the specified client.
       /// </summary>
       ///<param name="client">The client.
       /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 22:34:04</remarks>
       public void Remove(string client)
       {
           BaseRemove(client);
       }
 
       /// <summary>
       /// Clears this instance.
       /// </summary>
       /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 22:34:29</remarks>
       public void Clear()
       {
           BaseClear();
       }
   }</see>

最后是最外层的group:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/// <summary>
   /// Class MailServerSection 为入口:
   /// </summary>
   /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 21:41:02</remarks>
   public class MailServerSection : ConfigurationSection   //继承配置文件中节
   {
       /// <summary>
       /// Gets the provider.:映射mailServerGroup节点的provider
       /// </summary>
       /// <value>The provider.</value>
       /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 22:05:59</remarks>
       [ConfigurationProperty("provider", IsKey = true)]
       public string provider { get { return this["provider"] as string; } }
 
       /// <summary>
       /// Gets or sets the mail servers.:映射新添加的节点mailServers节点;这个节点下还包含了若干个mailServer节点,因此它是一个集合类
       /// </summary>
       /// <value>The mail servers.</value>
       /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 22:05:56</remarks>
       [ConfigurationProperty("mailServers", IsDefaultCollection = false)]
       public MailServerCollection MailServers
       {
           get
           {
               return this["mailServers"] as MailServerCollection;
           }
           set
           {
               this["mailServers"] = value;
           }
 
       }
   }

同样,关联处理类和节点:

1
 

之后做个测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Program
    {
        static void Main(string[] args)
        {
            Test();
 
        }
 
        /// <summary>
        /// Tests this instance.:读取节点值示例
        /// </summary>
        /// <remarks>Editor:v-liuhch CreateTime:2015/6/27 23:04:53</remarks>
        private static void Test() {
 
            MailServerSection mailSection = (MailServerSection)ConfigurationManager.GetSection("mailServerGroup");
            Console.WriteLine("MailServerSection 的provider属性值:"+mailSection.provider);
            foreach (MailServerElement config in mailSection.MailServers)
            {
                Console.WriteLine("----------------------------------");
                Console.WriteLine("client值为:"+config.Client);
                Console.WriteLine("address值为:"+config.Address);
                Console.WriteLine("username值为:"+config.UserName);
                Console.WriteLine("password值为:"+config.Password);
                Console.WriteLine("----------------------------------");
            }
 
            Console.ReadKey();
 
        }
 
    }

http://www.2cto.com/kf/201506/412300.html

.Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点的更多相关文章

  1. .Net 配置文件--继承ConfigurationSection实现自定义处理类处理自定义配置节点

    除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...

  2. .Net 配置文件——继承ConfigurationSection实现自己定义处理类处理自己定义配置节点

    除了使用继承IConfigurationSectionHandler的方法定义处理自己定义节点的类.还能够通过继承ConfigurationSection类实现相同效果. 首先说下.Net配置文件里一 ...

  3. [转]掌握 ASP.NET 之路:自定义实体类简介 --自定义实体类和DataSet的比较

    转自: http://www.microsoft.com/china/msdn/library/webservices/asp.net/CustEntCls.mspx?mfr=true 发布日期 : ...

  4. 自定义 Mysql 类 与 自定义 异常类

    import MySQLdb class MyExcept(Exception): ''' 常见做法定义异常基类,然后在派生不同类型的异常 ''' def __init__(self, *args): ...

  5. tp5 ThinkPHP5 自定义异常处理类

    在项目的开发过程中异常抛出尤为重要不仅能够做出友好提示帮助掩盖我们伟大的程序员们尴尬的瞬间,还能做到提示开发人员代码白编写的错误,下面进行自定义异常抛出类,纯属个人理解,希望大家指正 首先在框架中我们 ...

  6. Spring自定义转换类,让@Value更方便

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 关于配置的文章已经写了很多,相信看过的人还是会有一定收获的,系列文章可阅读:南瓜慢说-配置相关文章.对于@Val ...

  7. 安卓开发28:自定义View类

    自定义View类 通过自定义View类,可以自定义复杂的,按照自己需求的控件. 一个简单的例子 mainActivity.java 这个里面就是最普通的代码,但是给自定义的控件加上了一个onclick ...

  8. [转]通过继承ConfigurationSection,在web.config中增加自定义配置

    本文转自:http://www.blue1000.com/bkhtml/2008-02/55810.htm 前几天写了一篇使用IConfigurationSectionHandler在web.conf ...

  9. 自定义继承于Page的基类

    自定义继承于Page的基类:MyBasePage[校验用户是否登录,如果登录则获取用户信息,否则跳转到登录页面]============================================ ...

随机推荐

  1. Codeforces Round #385 (Div. 2) B - Hongcow Solves A Puzzle 暴力

    B - Hongcow Solves A Puzzle 题目连接: http://codeforces.com/contest/745/problem/B Description Hongcow li ...

  2. Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分

    C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...

  3. memcached命令

    memcached相对于redis来说,简直简单太多,命令也少很多,一般应用都是使用redis,但了解一下也还是不错的. 具体命令和用法很参见:http://www.runoob.com/memcac ...

  4. 转:windows下多线程通信方法

    多线程知识简介 同一进程中可以包含多个线程,由于进程中的多个线程可以共享进程中的资源,所以使同一进程中的多个线程之间通信相对比较简单. 当需要有多个线程来访问一个全局变量时,通常我们会在这个全局变量前 ...

  5. 安装与配置 Elasticsearch

    环境:centos6.7 #查询已经安装的JDK rpm -qa | grep jdk #卸载 yum -y remove  java-1.8.0-openjdk-headless-1.8.0.91- ...

  6. iOS开发——iOS学习路线

    iOS学习路线 版权声明:欢迎转载,请贴上源地址:http://www.cnblogs.com/iCocos/(iOS梦工厂) 一:自学初步学习路线 二:高级完整学习路线 三:完整知识与能力体系 思维 ...

  7. 单击HighCharts柱形体弹框显示详细信息

    上篇博客介绍了如何在HighCharts统计图表下生成表格,二者相互结合,可以对数据进行更好的统计分析.在统计的同时,如果需要想要及时查看详细信息的话,就得再加一个功能,就是单击柱形体,显示该项下的详 ...

  8. Ubuntu14.04安装中文输入法以及解决Gedit中文乱码问题

    1 设置中文显示环境 1. 打开System Settings 2. 打开Personal-> Language Support. 会弹出如下对话框,提示你“语言支持没安装完整”. 点击“Rem ...

  9. 查看、关闭linux自启动网络服务

    1.查看 netstat --tulnp ..master  smtp 服务 2.关闭 /etc/init.d/服务 stop 停止 : start 启动 chkconfig 服务 off  关闭   ...

  10. android中的提示信息显示方法(toast应用)

    android中的提示信息显示方法(toast应用) (2011-10-17 11:02:06) 转载▼ 标签: android toast 杂谈 分类: Android android中toast的 ...