S
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
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