读取ConfigurationSectionGroup中的属性

时间:2011-06-29 22:23:29

标签: c# .net asp.net web-config app-config

请考虑.NET .config文件中的以下配置组。

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

支持类是:

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;
        }
    }

如何实现MySettingsConfigurationGroup上的Enabled属性?

1 个答案:

答案 0 :(得分:1)

我认为部分组不是按照您尝试的方式进行自定义的。更好的解决方案是简单地定义您自己的配置部分,该部分本身包含其他配置,并且完全省略了部分组的使用。然后,您将获得配置部分提供的完全灵活性。