C# - ConfigurationSection isRequired 属性

标签 c# configuration config

我遇到了这个奇怪的问题...在我的代码中,无论我将 IsRequired 的值设置为 false 还是 true,它都会保持为 false。但是,如果我输入 DefaultValue,它会起作用吗?

非工作代码是:

public class FtpSettingsSection : ConfigurationSection
{
    [ConfigurationProperty("host", IsRequired = true)]
    public HostElement Host
    {
        get { return (HostElement)this["host"]; }
        set { this["host"] = value; }
    }

}

public class HostElement : ConfigurationElement
{
    [ConfigurationProperty("URL", IsRequired = true)]
    public string URL
    {
        get { return (string)this["URL"]; }
        set { this["URL"] = value; }
    }
}

工作代码是:

public class FtpSettingsSection : ConfigurationSection
{
    [ConfigurationProperty("host", DefaultValue = "", IsRequired = true)]
    public HostElement Host
    {
        get { return (HostElement)this["host"]; }
        set { this["host"] = value; }
    }

}

public class HostElement : ConfigurationElement
{
    [ConfigurationProperty("URL", DefaultValue = "", IsRequired = true)]
    public string URL
    {
        get { return (string)this["URL"]; }
        set { this["URL"] = value; }
    }
}

为什么我需要将 DefaultValue 设置为“”?

最佳答案

我遇到了同样的问题,在这里找到了解决方案http://msdn.microsoft.com/en-us/library/system.configuration.configurationpropertyattribute%28v=vs.90%29.aspx#1 . ConfigurationPropertyAttribute 上的评论并不完全正确,但它解释了问题的基本原理:

The IsRequired member of ConfigurationPropertyAttribute does not work when applied to a child object (deriving from ConfigurationElement). When the subsystem reflects over the attributes of the parent section/element to determine which configuration properties are defined it will create a new instance (of the appropriate type) for each child element and store it in the parent's value list. Later, when it validates whether all required properties have been specified or not, it cannot differentiate between a default initialized child element and one that was explicitly contained in the configuration file.

The most ideal workaround would be to programmatically declare the required elements through the ConfigurationProperty class. This requires substantial more work than the declarative approach. An alternative...

据我所知,替代方案是不正确的。

可以在 ConfigurationProperty 页面上找到编程模型的示例。通过在我的自定义元素的构造函数中声明我需要的属性并保持其他一切不变,我设法为自己解决了这个问题。

我怀疑你添加 DefaultValue 时它实际上并没有工作,而是出于不同的原因抛出异常。您必须深入到 InnerException 链的末尾才能找到 ConfigurationErrorsException。缺少必需属性时的正确消息是 “未找到必需属性‘host’。(C:\path\to\yourproject\bin\Debug\yourproject.vshost.exe.Config line ##)”

当您提供空字符串默认值时,配置子系统将尝试将该字符串解析为 HostElement 并失败。生成的 ConfigurationErrorsException 包含消息 “无法解析属性‘host’的默认值。错误是:未将对象引用设置为对象的实例。(C:\path\to\yourproject\bin\Debug\yourproject.vshost.exe.Config 行 ##)"

关于C# - ConfigurationSection isRequired 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4091693/

相关文章:

c# - 如何正式定义这个 'var' 类型呢?

c# - 线程,两个线程之间的通信c#

configuration - Glassfish/Java EE 5 Web 服务的应用程序配置文件

.net - WCF 即服务 : Modifying app. 配置文件

Magento:以编程方式更改小部件的配置

c# - Entity Framework 迁移: Sequence contains more than one matching element

c# - .NET Remoting 和多线程查询

c - Lua 在 PPC Micro 上的足迹

c# - .NET Core 应用程序中的 ConfigurationManager

java - 如何为Java编写conf文件?