zoukankan      html  css  js  c++  java
  • c# 配置文件之configSections配置(二)

    在很多时候我们需要自定义我们自己的自定义App.config 文件,而微软为我们提供了默认的

    System.Configuration.DictionarySectionHandler

    System.Configuration.NameValueSectionHandler      

    System.Configuration.SingleTagSectionHandler

    DictionarySectionHandler使用

      DictionarySectionHandler的工作方式与NameValueFileSectionHandler几乎相同,其区别是DictionarySectionHandler返回HashTable对象,而后者返回的是NameValueCollection。

    1 <configSections>
    2   <section name="mailServer" type="System.Configuration.DictionarySectionHandler,System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    3 </configSections>
    4 <mailServer>
    5   <add key="url" value="mail.163.com"/>
    6   <add key="username" value="admin"/>
    7   <add key="password" value="123456"/>
    8 </mailServer>

      使用代码

    1 IDictionary dic = ConfigurationManager.GetSection("mailServer") as IDictionary; 
    2 Console.WriteLine(dic);
    3 foreach (var key in dic.Keys)
    4 {
    5     Console.WriteLine("{0}:{1}", key, dic[key]);
    6 }
    7 Console.ReadKey();

    由于DictionarySectionHandler返回的是HashTable对象,而HashTable中的Key是唯一的。那么DictionarySectionHandler如果配置了相同的Key,后面的值会覆盖前面的值。

    还是上面的的例子,我们将配置文件修改一下

    1 <mailServer>
    2   <add key="url" value="mail.163.com"/>
    3   <add key="username" value="admin"/>
    4   <add key="password" value="123456"/>
    5   <add key="password" value="12345678"/>
    6 </mailServer>

    接下来看看输出结果:

    NameValueSectionHandler使用

      xml配置

    1 <configSections>
    2   <section name="mailServer" type="System.Configuration.NameValueSectionHandler,System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    3 </configSections>
    4 <mailServer>
    5   <add key="url" value="mail.163.com"/>
    6   <add key="username" value="admin"/>
    7   <add key="password" value="123456"/>
    8   <add key="password" value="12345678"/>
    9 </mailServer>

      代码:

    1 NameValueCollection mailServer = ConfigurationManager.GetSection("mailServer") as NameValueCollection;
    2 Console.WriteLine(mailServer);
    3 foreach (var key in mailServer.AllKeys)
    4 {
    5     Console.WriteLine("{0}:{1}",key,mailServer[key]);
    6 }
    7 Console.ReadKey();

      输出结果:

    SingleTagSectionHandler使用

      SingleTagSectionHandler和DictionarySectionHandler一样,同样返回的是Hashtable对象,只是书写结构不一样。

      xml配置:

    1 <configSections>
    2   <section name="mailServer" type="System.Configuration.SingleTagSectionHandler,System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    3 </configSections>
    4 <mailServer url="mail.163.com" username="admin" password="12345678"/>

      代码:

    1 IDictionary mailServer = ConfigurationManager.GetSection("mailServer") as Hashtable ;
    2 Console.WriteLine(mailServer);
    3 foreach (var key in mailServer.Keys)
    4 {
    5     Console.WriteLine("{0}:{1}", key, mailServer[key]);
    6 }    
    7  Console.ReadKey();

      输出结果:

  • 相关阅读:
    Effective Java 第三版——72. 赞成使用标准异常
    Effective Java 第三版——71. 避免不必要地使用检查异常
    Effective Java 第三版——70. 对可恢复条件使用检查异常,对编程错误使用运行时异常
    Effective Java 第三版——69. 仅在发生异常的条件下使用异常
    Effective Java 第三版——68. 遵守普遍接受的命名约定
    Effective Java 第三版——67. 明智谨慎地进行优化
    Effective Java 第三版——66. 明智谨慎地使用本地方法
    Effective Java 第三版——65. 接口优于反射
    Effective Java 第三版——64. 通过对象的接口引用对象
    Effective Java 第三版——63. 注意字符串连接的性能
  • 原文地址:https://www.cnblogs.com/caoyc/p/6000694.html
Copyright © 2011-2022 走看看