Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ConfigurationSection and ConfigurationProperty validation

Status
Not open for further replies.

koleraba

Programmer
Jun 14, 2008
8
0
0
SI
Hi.

I created a CustomSection class by extending the ConfigurationSection. I red quite a few articles on the subject and they all sugest the same approach, but it doesn't work for me, and I dont have a clue what am I doing wrong. So any idea or help will be appreciated. Below is a simplified code:

namespace ConfigElementTest
{
public static class Globals
{
public static MySection Settings;

public static void Load()
{
try
{
Settings = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).GetSection("mySection") as MySection;
}
catch (Exception ex)
{

}
}
}

public class MySection : ConfigurationSection
{
public MySection()
{

}

[ConfigurationProperty("name"), StringValidator(MaxLength = 10, MinLength = 3)]
public string Name
{
get{return (string) base["name"];}
set { base["name"] = value; }
}
}
}

And here is my app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="mySection" type="ConfigElementTest.MySection, ConfigElementTest" />
</configSections>
<mySection name="Some Name" />
</configuration>

When I call the Globals.Load() method an exception is thrown saying that the name has to be at least 3 characters long. If I remove the validator, everything works fine and if i call Globals.Settings.Name I get the right value - "Some Name" - as specified in app. config

Thanks in advance,
Uros
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top