[英]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.