3

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

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

The supporting classes are:

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?

JCallico
  • 1,446
  • 18
  • 25

1 Answers1

1

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.

dana
  • 17,267
  • 6
  • 64
  • 88
NathanAldenSr
  • 7,841
  • 4
  • 40
  • 51