考虑 .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 属性?
最佳答案
我不认为分区组的设计目的是按照您尝试的方式进行自定义。更好的解决方案是简单地定义您自己的配置部分,该部分本身包含其他配置,并完全省略部分组的使用。然后,您将获得配置部分提供的全部灵 active 。
关于c# - 读取 ConfigurationSectionGroup 中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6527969/