zoukankan      html  css  js  c++  java
  • C# 快捷使用自定义配置节点

      C#除了appSettings和connectionStrings默认配置外还允许用户自定义使用配置。C# 提供3中简单的自定义配置,配置文件如下

      

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="Config1" type="System.Configuration.SingleTagSectionHandler"/>
        <section name="Config2" type="System.Configuration.DictionarySectionHandler"/>
        <section name="Config3" type="System.Configuration.NameValueSectionHandler"/>
      </configSections>
      <Config1 a="1" b="2"/>
      <Config2>
        <add key="a" value="1"/>
        <add key="b" value="2"/>
      </Config2>
      <Config3>
        <add key="a" value="1"/>
        <add key="b" value="2"/>
      </Config3>
    </configuration>

      使用这3种中配置要注意:

       1.  configSections节点必须要是配置的第一个节点。

       2. 自定义配置要在configSections内部声明。格式是<section name="配置名" type="类的全路径,dll名"/> 系统的这3种配置dll省略。

       3. 针对以上3种配置格式不能变,不能变,不能变,重要的事说3遍 。<Config1 a="1" b="2"></Config1 > 这种写法 抛出异常。

      这3中使用也比较简单,用ConfigurationManager.GetSection(XXX)获取节点数据。第一种和第二种都返回Hashtable类型(Hashtable实现IDictionary接口),第三种返回NameValueCollection 与appSettings返回类型相同。

      

     1  public static void Main(string[] args)
     2         {
     3             //    Hashtable
     4             Hashtable config1 = (Hashtable)ConfigurationManager.GetSection("Config1");
     5             Dictionary<string, string> c = new Dictionary<string, string>();
     6             Console.WriteLine("****************配置1**********************");
     7             Console.WriteLine("遍历");
     8             foreach (DictionaryEntry g in config1)
     9             {
    10                 Console.WriteLine(g.Key + "=" + g.Value);
    11             }
    12             Console.WriteLine("使用:a=" + config1["a"]);
    13 
    14 
    15             Hashtable config2 = (Hashtable)ConfigurationManager.GetSection("Config2");
    16             Console.WriteLine("****************配置2**********************");
    17             Console.WriteLine("遍历");
    18             foreach (DictionaryEntry g in config1)
    19             {
    20                 Console.WriteLine(g.Key + "=" + g.Value);
    21             }
    22             Console.WriteLine("使用:a=" + config1["a"]);
    23 
    24 
    25             
    26             Console.WriteLine("****************配置3**********************");
    27             NameValueCollection config3 = (NameValueCollection)ConfigurationManager.GetSection("Config3");
    28             Console.WriteLine("遍历");
    29             foreach (var item in config3.AllKeys)
    30             {
    31                 Console.WriteLine(item + "=" + config3[item]);
    32             }
    33             Console.WriteLine("使用:a=" + config3["a"]);
    34 
    35             Console.ReadLine();
    36         }
    获取配置

     调试结果:

      接下来我们来讲讲如何使用自己格式的配置方法,

      一.建立一个配置文件 如下

      

     1 <?xml version="1.0" encoding="utf-8" ?>
     2 <configuration>
     3   <configSections>
     4     <section name="MyConfig" type="CustomConfig.MyConfig.ConfigHandler,CustomConfig" />
     5   </configSections>
     6   
     7   <MyConfig c="3">
     8     <a>1</a>
     9     <b>2</b>
    10   </MyConfig>
    11   
    12     
    13 </configuration>

      确定数据格式和解析类。

      二.解析类

      

     1 namespace CustomConfig.MyConfig
     2 {
     3     public class ConfigHandler : IConfigurationSectionHandler
     4     {
     5         public object Create(object parent, object configContext, XmlNode section)
     6         {
     7             ConfigModel model = new ConfigModel //根据需要的类型来返回
     8             {
     9                 a = section.SelectSingleNode("/MyConfig/a").InnerText,
    10                 b = section.SelectSingleNode("/MyConfig/b").InnerText,
    11                 c = section.SelectSingleNode("/MyConfig").Attributes.GetNamedItem("c").Value
    12             };
    13             return model;
    14         }
    15     }
    16 
    17     public class ConfigModel
    18     {
    19         public string a { get; set; }
    20         public string b { get; set; }
    21         public string c { get; set; }
    22     }
    23 }

      1.配置解析类要实现 IConfigurationSectionHandler j接口  Create函数为具体解析函数,XmlNode section 包含节点全部数据

      2.确定数据的返回格式如 ConfigModel,可根据需要编写。

      3.解析方法和解析Xml相同。

       三. 取配置数据

      方法与之前相同一样用ConfigurationManager.GetSection(XXX)函数 ,执行时会自动调用解析类。

      

    public static void Main(string[] args)
            {
                ConfigModel config = (ConfigModel)ConfigurationManager.GetSection("MyConfig");
                Console.WriteLine(config.a + "," + config.b + "," + config.c);
            }

      

      

  • 相关阅读:
    SQL Server 错误15401:没有找到Windows NT用户或组‘EMLE\ASPNET’
    使用 TestContext 类
    C# 动态2维数组
    2010年春季 软件测试技术 软件项目管理 实验安排
    代码覆盖率结果:没有为此测试运行启用代码覆盖率
    InternetGetCookie/InternetSetCookie (WinInet) changed with Internet Explorer 7
    C# 替换文本文件中的某一行 (要求此文件存在)
    android 复制 粘贴 功能实现
    java动态代理(JDK和cglib)
    使用ant打web应用更新包
  • 原文地址:https://www.cnblogs.com/linxingxunyan/p/7077824.html
Copyright © 2011-2022 走看看