开发者

Reading attributes in ConfigurationSectionGroup

开发者 https://www.devze.com 2023-03-16 13:46 出处:网络
Consider the following configuration group in a .NET .config file. <MySettingsGroup enabled=\"true\">

Consider the following configuration group in a .NET .config file.

<MySettingsGroup enabled="true">
 <MySettingsSection enabled="true">
 </MySettingsSection>
</MySettingsGroup>

The supporting classes are:

开发者_如何学Python
public class MySettingsConfigurationSection : ConfigurationSection
{
    [ConfigurationProperty("enabled", DefaultValue = true, IsRequired = false)]
    public bool Enabled
    {
        get
        {
            // works fine
            return Convert.ToBoolean(this["enabled"]);
        }
    }

public class MySettingsConfigurationGroup : ConfigurationSectionGroup
{
    [ConfigurationProperty("enabled", DefaultValue = true, IsRequired = false)]
    public bool Enabled
    {
        get
        {
            // the way to retrieve attributes in sections is not supported by groups
            // return Convert.ToBoolean(this["enabled"]);
            return true;
        }
    }

How can the Enabled property on the MySettingsConfigurationGroup be implemented?


I don't think section groups were designed to be customized in the way you're attempting. A better solution would be to simply define your own configuration section that itself contains other configurations and omit the use of a section group altogether. Then, you'd get the full flexibility that configuration sections offer.

0

精彩评论

暂无评论...
验证码 换一张
取 消