繁体   English   中英

ConfigurationElement 设置空默认值

[英]ConfigurationElement set null default value

我在 App.config 中的自定义配置部分有一个小问题。 问题是:我有一个 CustomElement(我们称之为“a”),其属性 CustomElement 是另一个(我们称之为“b”)。

仅当指定为次要元素时,我才希望 b 作为值不为空。

例如

在这种情况下,我有 ab 不为空,这没关系。

<a prop_a1="" prop_a2="">
    <B prop_b1 = "" />
</ A>

但是,在以下情况下, b 应该为空,但事实并非如此。

<a prop_a1="" prop_a2="" />

在情况 2 中是否可能有空值?

感谢所有愿意帮助我的人。

public class Cfg : ConfigurationElement
{
        [ConfigurationProperty("prop", IsRequired = true)]
        public ErrorCfg Prop
        {
            get { return (string)this["prop"]; }
            set { this["prop"] = value; }
        }
}

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

    [ConfigurationProperty("prop2", IsRequired = true)]
    public string Prop2
    {
        get { return (string)this["prop2"]; }
        set { this["prop2"] = value; }
    }
}

if (_cfgItem.ErrorCfg != null)
{....}

在这种情况下 ErrorCfg 不为空,但 Prop1 和 Pro2 为空字符串

我认为这是不可能的。 但是您可以查明配置文件中是否存在ConfigurationElement

if (_cfgItem.ErrorCfg.ElementInformation.IsPresent)
{....}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM