Home All Groups Group Topic Archive Search About

Custom configuration section: How to get null for non required properties?

Author
2 Jan 2007 10:13 PM
style
In the following sample I declared a custom configuration section within my
app.config. As you can see, the lastName attribute of the second employee
element is missing:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="Employees" type="CustomConfigurationTest.MyConfigSection,
CustomConfigurationTest" />
  </configSections>
  <Employees>
    <Employee EmployeeID="1" FirstName="John" LastName="Doe" />
    <Employee EmployeeID="2" FirstName="Jane" />
  </Employees>
</configuration>

The corresponding ConfigurationElement derived class looks as follows:

public class EmployeeElement : ConfigurationElement
{
    [ConfigurationProperty("EmployeeID", IsKey = true, IsRequired = true)]
    public int EmployeeID
    {
        get { return (int)this["EmployeeID"]; }
        set { this["EmployeeID"] = value; }
    }
    [ConfigurationProperty("FirstName", IsRequired = true)]
    public string FirstName
    {
        get { return (string)this["FirstName"]; }
        set { this["FirstName"] = value; }
    }
    [ConfigurationProperty("LastName", IsRequired = false)]
    public string LastName
    {
        get { return (string)this["LastName"]; }
        set { this["LastName"] = value; }
    }
}

Please note that the LastName property is marked as "IsRequired=false". At
runtime the LastName property contains an empty string for the second
employee because the lastName attribute isn't specified in the app.config.
But I'd rather like to get a null value instead. Does the .NET configuration
mechanism somehow allow to get a null value for non required configuration
element properties when the corresponding attribute isn't defined? Or is
this just not possible?

Any hints are appreciated.

Best regards

Author
3 Jan 2007 8:29 AM
andyk
You could try specifying a default value of null:

[ConfigurationProperty("LastName", IsRequired = false, DefaultValue =
null)]
public string LastName
{
    get { return (string)this["LastName"]; }
    set { this["LastName"] = value; }
}

style wrote:
Show quote
> In the following sample I declared a custom configuration section within my
> app.config. As you can see, the lastName attribute of the second employee
> element is missing:
>
> <?xml version="1.0" encoding="utf-8" ?>
> <configuration>
>   <configSections>
>     <section name="Employees" type="CustomConfigurationTest.MyConfigSection,
> CustomConfigurationTest" />
>   </configSections>
>   <Employees>
>     <Employee EmployeeID="1" FirstName="John" LastName="Doe" />
>     <Employee EmployeeID="2" FirstName="Jane" />
>   </Employees>
> </configuration>
>
> The corresponding ConfigurationElement derived class looks as follows:
>
> public class EmployeeElement : ConfigurationElement
> {
>     [ConfigurationProperty("EmployeeID", IsKey = true, IsRequired = true)]
>     public int EmployeeID
>     {
>         get { return (int)this["EmployeeID"]; }
>         set { this["EmployeeID"] = value; }
>     }
>     [ConfigurationProperty("FirstName", IsRequired = true)]
>     public string FirstName
>     {
>         get { return (string)this["FirstName"]; }
>         set { this["FirstName"] = value; }
>     }
>     [ConfigurationProperty("LastName", IsRequired = false)]
>     public string LastName
>     {
>         get { return (string)this["LastName"]; }
>         set { this["LastName"] = value; }
>     }
> }
>
> Please note that the LastName property is marked as "IsRequired=false". At
> runtime the LastName property contains an empty string for the second
> employee because the lastName attribute isn't specified in the app.config.
> But I'd rather like to get a null value instead. Does the .NET configuration
> mechanism somehow allow to get a null value for non required configuration
> element properties when the corresponding attribute isn't defined? Or is
> this just not possible?
>
> Any hints are appreciated.
>
> Best regards
Author
3 Jan 2007 10:20 AM
style
Unfortunately this does not work. Any other ideas?

AddThis Social Bookmark Button