关于.net:Net-ConfigurationSection的简单使用

45次阅读

共计 3480 个字符,预计需要花费 9 分钟才能阅读完成。

应用 C# 进行利用开发时,有时须要在程序配置文件中增加自定义的配置项,此时能够应用 ConfigurationSection。

本文参考链接:ConfigurationSection、ConfigurationElement、ConfigurationElementCollection、ConfigurationProperty 及 James Johnson 的回复。文中有误的中央欢送大家交换斧正。

ConfigurationProperty 示意配置元素的一个个性或子级。能够设置其名称、类型和默认值,并能够指定该属性是否是必须的,是否为 Key 等。

ConfigurationElement 示意配置文件中的配置元素,每一条配置项对应一条 ConfigurationElement。ConfigurationElement 是抽象类,通过继承该类能够增加自定义配置属性。

ConfigurationElementCollection 示意配置文件中元素的汇合,蕴含一组配置项。ConfigurationElementCollection 继承 ConfigurationElement,并实现 ICollection 接口。

ConfigurationSection 示意配置文件中的节。通过继承该类能够不便的扩大自定义的节点信息,通过 ConfigurationManager.GetSection() 办法能够获取到自定义的 Section。

示例

// config 配置文件
<configSections>
    <section name="customSection" type="ConsoleApplication1.CustomConfigurationSection,ConsoleApplication1"/>
</configSections>
<customSection>
    <customElementsCollection>
        <customElement name="name1" content="content1"/>
        <customElement name="name2" content="content2"/>
        <customElement name="name3" content="content3"/>
    </customElementsCollection>
</customSection>

// ConfigurationElement
public class CustomConfigurationElement : ConfigurationElement
{public CustomConfigurationElement(string name, string content)
    {
        this.Name = name;
        this.Content = content;
    }

    public CustomConfigurationElement()
    { }

    [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
    public string Name
    {get { return (string)this["name"]; }
        set {this["name"] = value; }
    }

    [ConfigurationProperty("content", IsRequired = true)]
    public string Content
    {get { return (string)this["content"]; }
        set {this["content"] = value; }
    }
}

// ConfigurationElementCollection
public class CustomConfigurationElementCollection : ConfigurationElementCollection
{public CustomConfigurationElementCollection()
    { }

    protected override ConfigurationElement CreateNewElement()
    {return new CustomConfigurationElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {return ((CustomConfigurationElement)element).Name;
    }

    protected override bool IsElementName(string elementName)
    {
        bool isName = false;
        if(!string.IsNullOrEmpty(elementName))
            isName = elementName.Equals("customElement");

        return isName;
    }

    public override ConfigurationElementCollectionType CollectionType
    {get { return ConfigurationElementCollectionType.BasicMap;}
    }

    protected override string ElementName
    {get { return "customElement";}
    }

    public CustomConfigurationElement this[object key]
    {get { return (CustomConfigurationElement)BaseGet(key); }
    }

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

            BaseAdd(index, value);
        }
    }
}

// ConfigurationSection
public class CustomConfigurationSection : ConfigurationSection
{public CustomConfigurationSection()
    { }

    [ConfigurationProperty("customElementsCollection", IsDefaultCollection = false)]
    public CustomConfigurationElementCollection CustomElementsCollection
    {get { return (CustomConfigurationElementCollection)base["customElementsCollection"]; }
    }
}

// use
public static void ReadConfigurationSection(string sectionName)
{
    try
    {Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) as Configuration;
        var customSection = config.GetSection(sectionName) as CustomConfigurationSection;
        if(customSection == null)
        {Console.WriteLine($"read failed: {sectionName} is null");
            return;
        }

        Console.WriteLine("custom configuration section info:");
        for (int i = 0; i < customSection.CustomElementsCollection.Count; ++i)
        {Console.WriteLine($"\tName:{customSection.CustomElementsCollection[i].Name} Content:{customSection.CustomElementsCollection[i].Content}");
        }
    }
    catch (ConfigurationErrorsException err)
    {Console.WriteLine($"read configuration error: {err.Message}");
    }
}

正文完
 0