zoukankan      html  css  js  c++  java
  • C# 创建自定义配置节点1

    转载:http://www.educity.cn/develop/495003.html

          在.Net应用程序中我们经常看到VS为我们生成的项目工程中都会含有app.config或者web.connfig这样的文件.这个文件就是我们所说的应用程序配置文件.在这个文件里面记述着一些与我们的应用程序相关的信息如数据库连接认证模式等我们在程序中可以利用ConfigurationManager的ConnectionStrings属性方便的获取配置文件中的数据库连接字符串信息。

    可是有时候我们需要对它进行一些扩展加入一些自定义的元素而不是仅仅使用默认的配置.例如我们可能需要在程序启动时动态的加载某个类并对其进行初始化,而这个类或者初始化数据是我们在程序设计的时候所不知道的.相信大家都碰到过这样的问题,这里就不做过多的解释了.最好的办法无非就是把这些可能会改变的东西写进配置文件里面,到你能够确定的时候你只需要修改配置文件就Ok了,而不是产品马上上线的时候还用秒钟打开VS去改代码。

    添加一些自定义的元素到配置文件中是很容易的只需要两步就能搞定:

       1、 在<configSections>节点中注册你所要定义的节点名称及用于处理该节点的配置节处理程序代码如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="dbFactory"  type ="DbFactory.Configuration.DbFactorySection,DbFactory.Configuration" />
      </configSections>
    </configuration>

     

      2、 在适当的位置添加自定义的节点代码如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="dbFactory" type ="DbFactory.Configuration.DbFactorySection,DbFactory.Configuration" />
      </configSections>
      <dbFactory >
        <default factory="sql"></default>
        <factorys>
          <add name="sql"  assembly="HelloData"  class="SqlDbFactory" />
          <add name="oracle" assembly="HelloData" class="OracleDbFactory" />        
        </factorys>      
      </dbFactory>
    </configuration>
        
     自定义节点算是添加完了,可是我们怎么在程序里面获取这些配置信息呢?相信大家很多都是玩XML的高手,用SystemXml下的一些类写个XML的解析类把我们自定义的节点信息解析出来不就得到我们想要的东西了吗?的确是这样大牛果然是大牛小弟实在是佩服!可是如果用这种方式来实现的话小弟就实在没有必要写这篇博文了。

    Net框架为我们实现了很多的配置API来简化我们对配置文件的操作。在第一个步骤中我们提到了配置节处理程序这个程序就是用来读写我们自定义的节点信息的。那么我们又如何来实现一个配置节处理程序呢?首先我们来了解一下相关的类和概念。

    ConfigurationSection:自定义节点都要继承该类以提供对自定义配置节的自定义处理和编程访问。

    ConfigurationElement:它表示配置文件内的一个元素。

    ConfigurationElementCollection:它表示包含一个子元素集合的配置元素。

    有了这些类我们可以归纳起来配置文件中有两种类型的配置元素。

    第一:单一型配置元素即继承于ConfigurationElement的元素它不包含任何子元素。

    第二:集合型配置元素即继承于ConfigurationElementCollection的元素它包含一个子元素集合。

    概念往往都比较抽象,要搞清楚这些东西我们还是结合我们上面给的例子来具体说明一下。

    在<dbFactory> 配置节中有两个元素,即<default>和<factorys>而<factorys>元素又包含了两个子元素。那么在这里<default>就是单一型配置元素,而<factorys>就是集合型配置元素我们需要分别实现与这些元素相对应的类及其属性。

    <default>元素它是一个单一型的元素,所以我们继承ConfigurationElement该元素中有一个factory属性那么我们在类中进行相应的定义代码如下:

    代码

            public class DefaultElement : ConfigurationElement
            {
                [ConfigurationProperty("factory")]
                public string Factory
                {
                    get { return this["factory"] as string; }
                    set { this["factory"] = value; }
                }
            }

    注意在属性定义上面我们需要注册该属性的ConfigurationProperty特性。

    <factorys>子元素

    代码:

    public class FactoryElement : ConfigurationElement
            {
                [ConfigurationProperty("name")]
                public string Name
                {
                    get
                    {
                        return this["name"] as string;
                    }
                    set
                    {
                        this["name"] = value;
                    }
                }
                [ConfigurationProperty("assembly")]
                public string Assembly
                {
                    get
                    {
                        return this["assembly"] as string;
                    }
                    set
                    {
                        this["assembly"] = value;
                    }
                }
                [ConfigurationProperty("class")]
                public string Class
                {
                    get
                    {
                        return this["class"] as string;
                    }
                    set
                    {
                        this["class"] = value;
                    }
                }
            }
        }

    <factorys>元素是集合型元素继承ConfigurationElementCollection

    代码:

      public class FactoryElements : ConfigurationElementCollection
            {
                protected override ConfigurationElement CreateNewElement()
                {
                    return new FactoryElement();
                }
                protected override object GetElementKey(ConfigurationElement element)
                {
                    return ((FactoryElement)element).Name;
                }
                public FactoryElement this[string name]
                {
                    get
                    {
                        return BaseGet(name) as FactoryElement;
                    }
                }
            }

        ConfigurationElementCollection类是个抽象类,你应该显示的实现它的CreateNewElement方法和GetElementKey方法。

    <dbFactory>节点继承于ConfigurationSection

    代码

     

            public class DbFactorySection : ConfigurationSection
            {
                [ConfigurationProperty("default")]
                public DefaultElement DefaultFactory
                {
                    get { return this["default"] as DefaultElement; }
                    set { this["default"] = value; }
                }
                [ConfigurationProperty("factorys")]
    
                public FactoryElements Factorys
                {
                    get
                    {
                        return this["factorys"] as FactoryElements;
                    }
                    set
                    {
                        this["factorys"] = value;
                    }
                }
    
    
            }
         配置节处理程序终于写完了。把这四个类放在同一个工程目录下,编译成一个DLL,在你需要获取配置信息的地方引用这个DLL,用DbFactorySection section = ConfigurationManager.GetSection( “dbFactory”) as DbFactorySection;试试section是不是你想要的东西呢?
  • 相关阅读:
    spring中bean的高级属性之list, set, map以及props元素(含举例)
    Schema约束与DTD约束
    spring中配置Properties对象的方法
    web.xml 中的listener、 filter、servlet 加载顺序及其详解
    MySql为某个表增加rownumber
    分组取前N记录(转)
    1、Spring MVC的web.xml配置详解(转)
    Spring的PropertyPlaceholderConfigurer应用(转)
    几种任务调度的 Java 实现方法与比较(定时任务)(转)
    Spring定时任务的几种实现(转)
  • 原文地址:https://www.cnblogs.com/geduocoding/p/9279064.html
Copyright © 2011-2022 走看看