1

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

我希望只有在指定为辅助元素时才将 b 作为值而不是 null 。

例如

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

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

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

<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 为空字符串

4

1 回答 1

1

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

if (_cfgItem.ErrorCfg.ElementInformation.IsPresent)
{....}
于 2020-02-18T14:46:38.473 回答