【问题标题】:ConfigurationElement set null default valueConfigurationElement 设置 null 默认值
【发布时间】:2020-02-18 22:46:38
【问题描述】:

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

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

例如

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

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

【问题讨论】:

  • null和零不一样
  • 对不起,翻译错字,我的意思是空
  • 在第二个例子中,b 是什么值?并发布代码如何获取一个小的可验证示例的值
  • 感谢 EpicKip,我在问题文本中添加了代码。

标签: c# app-config custom-configuration configurationelement


【解决方案1】:

我认为这是不可能的。但是你可以查看ConfigurationElement是否存在于配置文件中:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-15
    • 2017-10-06
    • 2013-04-28
    • 2016-12-28
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多