2011-06-29 80 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

我不認爲章節組是按照您嘗試的方式進行定製的。一個更好的解決方案是簡單地定義你自己的配置部分,它本身包含其他配置,並完全省略使用部分組。然後,您將獲得配置部分提供的全部靈活性。