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

  • 相关阅读:
    Centos 7.X 安装JDK1.8
    CentOS 7.X 搭建时间服务器 --- chrony
    CentOS 7.X 设置系统时间
    CentOS 7.X 关闭SELinux
    C11工具类:时间处理
    C11内存管理之道:智能指针
    C11性能之道:标准库优化
    C11性能之道:转移和转发
    C11性能之道:右值引用
    C11简洁之道:tupe元祖
  • 原文地址:https://www.cnblogs.com/Aricc/p/1660376.html
Copyright © 2011-2022 走看看