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

  • 相关阅读:
    静态页面设置缓存、动态页面设缓存
    未能加载文件或程序集“WebGrease, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
    CS0016: 未能写入输出文件“c:WindowsMicrosoft.NETFrameworkv4.0.30319Temporary ASP.NET Files
    asp.net mvc 重定向
    win8 应用商店。 app下载的音乐和视频软件能打开,不能正常播放 解决方法
    JS 阻止事件冒泡
    ASP.NET MVC4空MVC项目添加脚本压缩和合并
    TabHost说明
    colors.xml
    MMU (一)
  • 原文地址:https://www.cnblogs.com/Aricc/p/1660376.html
Copyright © 2011-2022 走看看