Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ConfigurationProperty Validator

ConfigurationProperty Validator

Scheduled Pinned Locked Moved C#
questionregexhelp
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    koleraba
    wrote on last edited by
    #1

    Hi I have a question concerning custom ConfigurationValidator, which verifies the entries in the app.config file. Below is the class which represents the section in the config file.

    public class SettingSection : ConfigurationSection
    {
    [ConfigurationProperty("appId", IsKey=true, IsRequired=true), ConfigurationValidator(typeof(AppIdValidator))]
    public string AppId
    {
    get { return (string)base["appId"]; }
    }
    }

    public class AppIdValidator : ConfigurationValidatorBase
    {
    public override bool CanValidate(Type type)
    {
    return true;
    }

    public override void Validate(object value)
    {
       string val = (string)value;
       Regex regex = new Regex(@"\\A\\d{4}-\\d{4}-\\d{4}-\\d{4}\\z");
       if (regex.IsMatch(val))
       {
          return;
       }
       else
       {
          throw new FormatException("Input string is not in correct format");
       }
    

    }
    }

    The problem is when the instance of SettingSection is created, and the validator is called, the metod Validate is called with empty string - no metter what is the value in the app.config file. If I set the DefaultValue of the ConfigurationProperty attribute, than the validator is called with that value - again regardless to the entry in app.config. Any idea will be appreciated Uros

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups