·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP网站建设 >> 创建自定义配置节

创建自定义配置节

作者:佚名      ASP网站建设编辑:admin      更新时间:2022-07-23
 在.Net应用程序中,我们经常看到VS为我们生成的项目工程中都会含有App.config或者Web.config这样的文件。这个文件就是我们所说的应用程序配置文件。在这个文件里面记述着一些与我们的应用程序相关的信息,如:数据库连接,认证模式等。我们在程序中可以利用ConfigurationManager的ConnectionStrings属性方便的获取配置文件中的数据库连接字符串信息。

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

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

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



1   <configSections>2     <section name="dbFactory" type="DbFactory.Configuration.DbFactorySection,DbFactory.Configuration"/>3   </configSections>
  2. 在适当的位置添加自定义的节点。代码如下:



1   <configSections> 2     <section name="dbFactory" type="DbFactory.Configuration.DbFactorySection,DbFactory.Configuration"/> 3   </configSections> 4   <dbFactory > 5     <default factory="sql"></default> 6     <factorys> 7       <add name="sql"  assembly="Hello.Data"  class="SqlDbFactory" /> 8       <add name="Oracle" assembly="Hello.Data" class="OracleDbFactory" /> 9     </factorys>10   </dbFactory>
  自定义节点算是添加完了,可是我们怎么在程序里面获取这些配置信息呢?相信大家很多都是玩xml的高手,用System.Xml下的一些类写个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是不是你想要的东西呢?