纸上得来终觉浅,绝知此事要躬行。

 

【.NET】在config文件中自定义配置节

相关类:

ConfigurationSection

ConfigurationElementCollection

ConfigurationElement

完成后的配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
    <section name="JRogerServices" type="EnterpriseLibraryStudy.JRogerSection, EnterpriseLibraryStudy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowDefinition="Everywhere"/>
</configSections>
    
    <JRogerServices name="custom-section">
        <JRogerElement key="con" value="http://www.JRoger.NET/" />
        <Services>
            <clear />
            <add key="con" value="JRoger" />
        </Services>
    </JRogerServices>
</configuration>

实现:

1. 首先实现 ConfigurationElement 抽象类。注意 ConfigurationProperty 的 name 是区分大小写的。

public class JRogerElement : ConfigurationElement
{
    public JRogerElement() { }

    public JRogerElement(String key)
    {
        this.Key = key;
    }

    public JRogerElement(string key, string value)
    {
        this.Key = key;
        this.Value = value;
    }

    [ConfigurationProperty("key", IsKey = true)]
    public String Key
    {
        get { return (String)this["key"]; }
        set { this["key"] = value; }
    }

    [ConfigurationProperty("value")]
    public String Value
    {
        get { return (String)this["value"]; }
        set { this["value"] = value; }
    }
}

2. 然后实现 ConfigurationElementCollection 抽象类。

public class JRogerElementCollection : ConfigurationElementCollection
{
    public JRogerElementCollection()
    {
        
    }

    public JRogerElement this[int index]
    {
        get { return (JRogerElement)this.BaseGet(index); }
        set
        {
            if (this.BaseGet(index) != null)
            {
                this.BaseRemoveAt(index);
            }

            this.BaseAdd(index, value);
        }
    }

    public new JRogerElement this[String name]
    {
        get { return (JRogerElement)this.BaseGet(name); }
    }

    public void Clear() { this.BaseClear(); }

    //关键
    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.AddRemoveClearMap;
        }
    }

    #region Overrides of ConfigurationElementCollection

    /// <summary>
    /// 当在派生的类中重写时,创建一个新的 <see cref="T:System.Configuration.ConfigurationElement"/>/// </summary>
    /// <returns>
    /// 一个新的 <see cref="T:System.Configuration.ConfigurationElement"/>/// </returns>
    protected override ConfigurationElement CreateNewElement() { return new JRogerElement(); }

    protected override ConfigurationElement CreateNewElement(string elementName)
    {
        return new JRogerElement(elementName, "Data Source=.; Initial Catalog=TestDB; User Id=sa; Password=sa;");
    }

    /// <summary>
    /// 在派生类中重写时获取指定配置元素的元素键。
    /// </summary>
    /// <returns>
    /// 一个 <see cref="T:System.Object"/>,用作指定 <see cref="T:System.Configuration.ConfigurationElement"/> 的键。
    /// </returns>
    /// <param name="element">要为其返回键的 <see cref="T:System.Configuration.ConfigurationElement"/></param>
    protected override object GetElementKey(ConfigurationElement element) { return ((JRogerElement)element).Key; }

    #endregion
}

3. 最后实现 ConfigurationSection 抽象类。

public class JRogerSection : ConfigurationSection
{
    public JRogerSection()
    {
            
    }

    [ConfigurationProperty("Services")]
    public JRogerElementCollection JRogerService
    {
        get
        {
            return (JRogerElementCollection)this["Services"];
        }
    }

    [ConfigurationProperty("name")]
    public String Name
    {
        get { return (String)this["name"]; }
        set { this["name"] = value; }
    }

    [ConfigurationProperty("JRogerElement")]
    public JRogerElement CurrentConfig 
    {
        get { return (JRogerElement)base["JRogerElement"]; }
    }
} 

应用:

static void Main(string[] args)
{
    var section = ConfigurationManager.GetSection("JRogerServices") as JRogerSection;

    Console.WriteLine(section.Name);
    Console.WriteLine(section.CurrentConfig);
    Console.WriteLine(section.JRogerService);

    Console.ReadKey(true);
}

以上只是一个简单的示例,还可以实现对配置文件的修改。具体请参考MSDN。 

posted on 2014-02-20 17:35  JRoger  阅读(421)  评论(0编辑  收藏  举报

导航