2011-06-29 45 views
3

请考虑.NET .config文件中的以下配置组。阅读ConfigurationSectionGroup中的属性

<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

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