Hi.
I created a CustomSection class by extending the ConfigurationSection. I read 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 below is my app.config file
<?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