简体   繁体   中英

ConfigurationElement set null default value

I have a small problem with a custom configuration section in App.config. The problem is: I have a CustomElement (let's call it 'a') whose properties CustomElement another (let's call it 'b').

I would like to have b as a value not null only if specified as a secondary element.

Eg

In this case, I have ab as not null, and that's ok.

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

But, in the following case, b should be null, but it is not.

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

Is it possible to have a null value in case 2?

Thanks to all those who want to help me.

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)
{....}

In this case ErrorCfg is not null, but Prop1 and Pro2 are empty string

I think this is not possible. But you can find out if the ConfigurationElement is present in the configuration file:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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