Inherits the default values of ConfigurationValidatorBase and ConfigurationProperty.

Source: Internet
Author: User

By inheriting the ConfigurationValidatorBase class, you can customize a configuration system validators. However, this method is used today to identify a problem: before each ConfigurationProperty verifies its actual value, the default value must be verified first. Note that only one verification is performed here, before the value of the first ConfigurationProperty is verified. The representation is like a static constructor.

 

For example, the following ConfigurationSection and ConfigurationValidatorBase code :( the simulated verification code must be older than or equal to 15 years old; otherwise, an exception is thrown)

// Custom ConfigurationSection </p> <p> class AgeSection: ConfigurationSection </p> <p >{</p> <p> [ConfigurationProperty ("age "), age15Validator] </p> <p> public int Age </p> <p >{</p> <p> get {return (int) this ["age"] ;}</p> <p> set {this ["age"] = value ;} </p> <p >}</p> <p> // custom validators </p> <p> class Age15Validator: configurationValidatorBase </p> <p >{</p> <p> public override bool CanValidate (Type type) </p> <p >{</p> <p> return type = typeof (Int32 ); </p> <p >}</p> <p> public override void Validate (object value) </p> <p >{</p> <p> int age = (int) value; </p> <p> if (age <15) </p> <p> throw new ArgumentException ("the age must be greater than or equal to 15 "); </p> <p >}</p> <p> // authenticator Attribute </p> <p> class Age15ValidatorAttribute: configurationValidatorAttribute </p> <p >{</p> <p> public override ConfigurationValidatorBase ValidatorInstance </p> <p >{</p> <p> get </p> <p >{</p> <p> return new Age15Validator (); </p> <p >}</p> <p>}

Then app. config defines a valid and an invalid (AgeSection with Age less than 15) ConfigurationSection.

<? Xml version = "1.0" encoding = "UTF-8"?> </P> <configuration> </p> <configSections> </p> <section name = "mysec1" type = "Mgen. ageSection, Mgen "/> </p> <section name =" mysec2 "type =" Mgen. ageSection, mgen "/> </p> </configSections> </p> <mysec1 age =" 29 "/> </p> <mysec2 age = "4"/> </p> </configuration>
Test logic code:Try <br/> {</p> <p> var appconf = ConfigurationManager. openExeConfiguration (ConfigurationUserLevel. none); </p> <p> var sec1 = appconf. getSection ("mysec1") as AgeSection; </p> <p> if (sec1! = Null) </p> <p> Console. writeLine (sec1.Age); </p> <p> var sec2 = appconf. getSection ("mysec2") as AgeSection; </p> <p> if (sec2! = Null) </p> <p> Console. writeLine (sec2.Age); </p> <p >}</p> <p> catch (ConfigurationErrorsException ex) </p> <p >{</p> <p> Console. writeLine (ex. message); </p> <p>}

This code was initially taken for granted to output 29, because mysec1 will pass the verification and then receives the ConfigurationErrorsException, because mysec2 will not pass the verification. The result is that even 29 is not output, and the error message that failed verification is directly displayed. Check that the Line attribute of ConfigurationErrorsException is 0.

 

After debugging, we found that the number of times to run the Validate function of the validators (Age15Validator class in this example) is 3, rather than the expected 2. The input values are 0, 29, and 4. The value 0 is the default value of a type of ConfigurationProperty mentioned at the beginning of the article. Once again, it is emphasized that the verification of this default parameter is only called once and is limited to the actual value of any actual ConfigurationProperty.

 

To address the preceding problems, the solution is to set the default value of ConfigurationProperty and ensure that the default value can pass the verification by the validators, for example, we can change the default value of Age attribute of AgeSection to 15.

[ConfigurationProperty ("age", DefaultValue = 15 ), age15Validator] </p> <p> public int Age </p> <p >{</p> <p> get {return (int) this ["age"] ;}</p> <p> set {this ["age"] = value ;}</p> <p>}
In this way, the program can run as required.29 </p> <p> The value for the property 'age' is not valid. the error is: The age must be greater than or equal to 15 </p> <p> years old (E: \ My Documents ents \ Visual Studio 2008 \ Projects \ TTC \ bin \ Release \ Mgen.exe. con </p> <p> fig line 9)

The first line outputs 29 (the mysec1 attribute is verified)

The second line outputs an error message and specifies the number of rows. (Mysec2 failed verification)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.