我在App.config中的自定义配置部分遇到了一个小问题。问题是:我有一个CustomElement (让我们叫它'a'),它的属性CustomElement另一个(让我们叫它'b')。
我希望只有在指定为辅助元素时,b才是值not null。
例如
在这种情况下,我有一个b作为非空,这是可以的。
<a prop_a1="" prop_a2="">
<B prop_b1 = "" />
</ A>但是,在下面的例子中,b应该是空的,但它不是。
<a prop_a1="" prop_a2="" />在第二种情况下可以有一个空值吗?
感谢所有想要帮助我的人。
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不是null,但Prop1和Pro2是空字符串
发布于 2020-02-18 22:46:38
我认为这是不可能的。但您可以查看配置文件中是否存在该ConfigurationElement:
if (_cfgItem.ErrorCfg.ElementInformation.IsPresent)
{....}https://stackoverflow.com/questions/43493015
复制相似问题