热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> .Net编程>>正文内容

如何使用和开发自定义配置节

发布时间:2010年07月03日点击数: 佚名

在日常的程序设计中,如何灵活和巧妙地运用配置信息是一个成功的设计师的首要选择。这不仅是为了程序设计得更灵活性和可扩展性,也是为了让你的代码给人以清新的感觉。程序中的配置信息一般放在应用程序的app.config或web.config文件中,当然也可以自定义自己的配置文件。这些配置文件是以XML格式进行存储和读取的。微软也封装一些对这些配置文件操作的类,这些类存在于名字空间System.Configuration下,这个命名空间包含提供用于处理配置数据的编程模型的类型,当然为了使用还添加System.Configuration.dll程序集。

   现在我们先看一下这个名字空间下的几个重要的类:

   1、ConfigurationManager,这个提供用于打开客户端应用程序集的Configuration对象。

   2、WebConfigurationMaManager,这个提供用于打开web应用程序集的Configuration对象。

   3、ConfigurationSection ,表示配置文件中的节。

   4、ConfigurationSectionCollection ,表示配置文件中相关节的集合。

   5、ConfigurationSectionGroup ,表示配置文件中的一组相关节。

   6、ConfigurationSectionGroupCollection ,表示 ConfigurationSectionGroup 对象的集合。

   7、ConfigurationProperty ,表示属性或配置元素的子元素。

   8、ConfigurationPropertyAttribute ,以声明方式指示 .NET Framework,以实例化配置属性。

   9、ConfigurationElement ,表示配置文件中的配置元素。

   10、ConfigurationElementCollection ,表示包含一个子元素集合的配置元素。

  当然这里面这常用的是ConfigurationManager类,这个类提供了两个静态常用的静态方法:

       object GetSection(string sectionName)用于读取当前应用程序默认配置的指定配置信息;

        Configuration OpenExeConfiguration(ConfigurationUserLevel userLevel)将当前应用程序配置文件打开以得到一个Configuration对象。

     以及两个静态属性:AppSettins获取当前应用程序默认配置的AppSettingsSection数据;ConnectionStrings获取当前应用程序默认配置的ConnectionStringSection数据。

     当然由于AppSettings是一个NameValueCollection对象,因此对它的读取和设置可以直接以AppSettings[“mySet”]的形式得到,相应的在*.config文件中的<appsettings>节下添加<add name=”mySet” value=””/>即可。

    要自定义配置节配置元素,就必须要使我们的类分别继承自ConfigurationSection和ConfigurationElement类。那么实现这个两个类的对象就可以加入到配置文件的<configSection>和其他元素节点中。

    这儿介绍在配置节点中加入自定义配置信息的能力(元素节点)。

    1、实现一个继承自ConfigurationElement和ConfigurationSection的类,并添加的配置属性加上ConfigurationPropertyAttribute特性。

    2、添加配置信息。

    3、在程序中读取配置信息。

第一步:实现派生自ConfigurationSection和ConfigurationElement的类

  1. using System;  
  2. using System.Collections;  
  3. using System.Text;  
  4. using System.Configuration;  
  5. using System.Xml; 
  6. namespace MyCustomConfiguration  
  7. {  
  8.    
  9. //自定义配置节  
  10. public class CustomSectionConfiguration : ConfigurationSection  
  11. {  
  12.      public CustomSectionConfiguration() { }  
  13.      public CustomSectionConfiguration(string value)  
  14.      {  
  15.      }  
  16.      //添加特性ConfigurationPropertyAttribute  
  17.      //'customAttribute'是配置文件中的元素  
  18.      //'CustomAttribute'是程序中要的属性;  
  19.      [ConfigurationProperty("customAttribute", DefaultValue = "", IsRequired = true)]  
  20.      public string CustomAttribute {  
  21.          get { return (string)base["customAttribute"]; }  
  22.          set { base["customAttribute"] = value; }  
  23.      }  
  24.      //添加特性ConfigurationPropertyAttribute  
  25.      [ConfigurationProperty("customElement", DefaultValue = "", IsRequired = true)]  
  26.      public CustomElementConfiguration CustomElement {  
  27.          get { return (CustomElementConfiguration)base["customElement"]; }  
  28.          set { base["customElement"] = value; }  
  29.      } 
  30. }  
  31. //自定义配置元素  
  32. public class CustomElementConfiguration : ConfigurationElement  
  33. {  
  34.      public CustomElementConfiguration() { }  
  35.      public CustomElementConfiguration(string value1, string value2)  
  36.      {  
  37.          Value1 = value1;  
  38.          Value2 = value2;  
  39.      } 
  40.      [ConfigurationProperty("value1", DefaultValue = "", IsRequired = true)]  
  41.      public string Value1 {  
  42.          get { return (string)base["value1"]; }  
  43.          set { base["value1"] = value; }  
  44.      } 
  45.      [ConfigurationProperty("value2", DefaultValue = "", IsRequired = true)]  
  46.      public string Value2  
  47.      {  
  48.          get { return (string)base["value2"]; }  
  49.          set { base["value2"] = value; }  
  50.      }  

第二步:在*.config中设置自定的元素

  1. <configuration> 
  2. <!-- 配置节-->  
  3.   <configSections>  
  4.     <sectionGroup name="customGroup">  
  5.       <section  
  6.         name="customSection"  
  7.         type=" 
  8. MyCustomConfiguration.CustomSectionConfiguration, MyCustomConfiguration, Version=1.0.0.0, Culture=neutral
  9. PublicKeyToken=null"   allowLocation="true"    allowDefinition="everywhere"  />  
  10.     </sectionGroup> 
  11. ……  
  12.   <!-- 配置节设置信息 -->  
  13.   <customGroup>  
  14.     <customSection customAttribute="Custom">  
  15.       <customElement Value1=”best" Vaule2=”better”/>  
  16.     </customSection>  
  17.   </customGroup> 
  18. …… 
  19. </configuration> 
  20. 第三步:在程序中使用配置信息。 
  21.         首先得到配置节对象: 
  22. MyCustomConfiguration.CustomSectionConfiguration 
  23. config = (MyCustomConfiguration.CustomSectionConfiguration)System.Configuration.ConfigurationManager.GetSection(  
  24.             "customGroup/customSection");  
  25.         接着就可以使用强名称的对象和属性了。 

上面介绍的是单一属性的配置,如果要配置多个对象,那么就得使用System.Configuration.ConfigurationElementCollection,这类的作用是生成多个子对象,也就是在一个标签下可以放置多个System.Configuration.ConfigurationElement对象。
方法同上,我们必须要重写几方法:

  1. ConfigurationElement CreateNewElement()//这个方法的作用是返回子对象实例; 
  2. object GetElementKey(ConfigurationElement element);//这个方法的得到对象中的键名; 
  3. ConfigurationElementCollectionType CollectionType{get;}//这个属性是定义映射方式; 
  4. string ElementName{get;}//这个属性是定义XML元素的名字。 
  5. protected override ConfigurationElement CreateNewElement()  
  6.       {  
  7.           return new CustomElementConfiguration ();  
  8.       } 
  9.       protected override object GetElementKey(ConfigurationElement element)  
  10.       {  
  11.           return ((CustomElementConfiguration )element).Name;  
  12.       }  
  13.       public override ConfigurationElementCollectionType CollectionType  
  14.       {  
  15.           get {    return ConfigurationElementCollectionType.BasicMap;  }  
  16.       }  
  17.       protected override string ElementName  
  18.       {  
  19.           get {  return "collection"; }  
  20.       } 

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作