zoukankan      html  css  js  c++  java
  • [转]Custom Configuration Section Handler in .NET 2.0

     

    I recently had to deal with creating custom sections in the web/app.config file. In the .NET 1.x realm, I had to create a section handler which implements public class CustomSectionHandler: IConfigurationSectionHandler.

    In 2.0, we have to create a custom section handler that derives from ConfigurationSection, which then can be used to contain public properties that map the attributes in the configuration file.

    For example, Let's say that we have the following custom section in web.config:

       MySection Attribute1="1" Attribute2="x" />

    We can write a custom section like the following:

    using System.Configuration;

    public class MyConfigSection: ConfigurationSection

    {

       [ConfigurationProperty("Attribute1", IsKey = true, IsRequired = true)]

    public int Attribute1

       {

          get { return (int)this["Attribute1"]; }

          set { this["Attribute1"] = value; }

       }

       [ConfigurationProperty("Attribute2")]

       public string Attribute2

       {

          get { return (string)this["Attribute2"]; }

          set { this["Attribute2"] = value; }

       }

    }

    If you want to support mutiple elements within a config section, you can do so by creating another class that derives from ConfigurationElementCollection to maintain a collection of the configuration element. We can tweak the custom section in the web.config file a little bit to support this.

    MyConfigSection>

    Employees>

    Employee EmployeeID="alincoln" FirstName="abraham" LastName="lincoln" />

    Employee EmployeeID="gwashington" FirstName="george" LastName="washington" />

    Employee EmployeeID="gbush" FirstName="george" LastName="bush" />

    Employees>

    MyConfigSection>

    Then, we can write the custon section like this:

    public class MyConfigSection: ConfigurationSection

    {

       [ConfigurationProperty("Employees", IsDefaultCollection = true)]

       public EmployeeCollection Employees

       {

          get { return (EmployeeCollection) base["Employees"]; }

       }

    }

    public sealed class EmployeeElement : ConfigurationElement

    {

       [ConfigurationProperty("EmployeeID", IsKey = true, IsRequired = true)]

       public string EmployeeID

       {

          get { return (string)this["EmployeeID"]; }

          set { this["EmployeeID"] = value; }

       }

       [ConfigurationProperty("FirstName", IsRequired = true)]

       public string FirstName

       {

          get { return (string)this["FirstName"]; }

          set { this["FirstName"] = value; }

       }

       [ConfigurationProperty("LastName", IsRequired = true)]

       public string LastName

       {

          get { return (string)this["LastName"]; }

          set { this["LastName"] = value; }

       }

    }

    public sealed class EmployeeCollection : ConfigurationElementCollection

    {

       protected override ConfigurationElement CreateNewElement()

       {

          return new EmployeeElement();

       }

       protected override object GetElementKey(ConfigurationElement element)

       {

          return ((EmployeeElement)element).EmployeeID;

       }

       public override ConfigurationElementCollectionType CollectionType

       {

          get { return ConfigurationElementCollectionType.BasicMap; }

       }

       protected override string ElementName

       {

          get { return "EmployeeElement"; }

       }

       public EmployeeElement this[int index]

       {

          get   {      return (EmployeeElement)BaseGet(index);   }

          set   

          {

                if (BaseGet(index) != null)

             {

                   BaseRemoveAt(index);

             }

             BaseAdd(index, value);

          }

       }

       new public EmployeeElement this[string employeeID]

       {

          get{return (EmployeeElement)BaseGet(employeeID);}

       }

       public bool ContainsKey(string key)

       {

             bool result = false;

             object[] keys = BaseGetAllKeys();

             foreach (object obj in keys)

             {

                   if ((string)obj == key)

                   {

                      result = true;

                      break;

                   }

             }

             return result;

       }

    }

    I've added the ContainsKey() Method to the EmployeeCollection class because I couldn't find a simpler way to check for the existence of a given key. Maybe there is a better solution..

    Now you need to add the custom section to the ConfigSections in the coniguration file.

    section name="MyConfigSection" type="Namespace.ClassName, AssemblyName" />

    Lastly, you can access the config section from your code the following way:

    MyConfigSection employeeSection=

    (MyConfigSection )System.Configuration.ConfigurationManager.GetSection("MyConfigSection");

    I hope it helps!

    Published Tuesday, August 08, 2006 8:18 AM by Jason Jung

    原文:http://blogs.neudesic.com/blogs/jason_jung/archive/2006/08/08/208.aspx

  • 相关阅读:
    linux Apache 安装与配置
    从零开始的JAVA 2. java程序的构成及命名规则
    从零开始的JAVA 1. 安装 JAVA 与 设置环境变量
    通过系统光盘搭建本地yum仓库
    Linux网站服务Apache+php+mysql的安装
    PostgreSQL学习手册(常用数据类型)
    1.4、WebRTC源码
    1.3、WebRTC架构
    1.2、初识WebRTC
    必读前言—— 作者的独白
  • 原文地址:https://www.cnblogs.com/Aricc/p/1660376.html
Copyright © 2011-2022 走看看