c# - 读取 ConfigurationSectionGroup 中的属性

标签 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 属性?

最佳答案

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

关于c# - 读取 ConfigurationSectionGroup 中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6527969/

相关文章:

c# - 这个 LINQ 表达式可以从方法语法转换为查询语法吗?

.net - 当 Windows 窗体应用程序正常时,无法从控制台应用程序创建 COM 对象

c# - 什么是 Azure Blob 上传异常?

c# - 在 ASP.net c# web 应用程序中显示警告框

javascript - 使用 setOptions 时 Highcharts 未引用错误

c# - C++ SDK 集成到 C# 程序中

c# - 使用 C# Windows 应用程序将数据存储在本地数据库中,并使本地数据库对用户隐藏

javascript - 如何在 asp.net C# 中将文本框文本更改为另一个文本框?

c# - ActionFilter - 发送 HTTP header 后无法重定向

c# - 固定间隔后刷新更新面板