开发者

Reading attributes in ConfigurationSectionGroup

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜