Geeks With Blogs
Sean Carpenter Thoughts on Development

I was recently working on creating a custom configuration section (deriving from ConfigurationSection) to provide some configuration metadata to an app I’m working on.  In creating a ConfigurationElement, I wanted to validate that one of the attributes of the element was a valid value for the application.  While looking at the example here I noticed the use of various validation attributes applied to validate the format of configuration values.

The CallbackValidatorAttribute and CallbackValidator seemed like they would fit the bill for doing custom validation.  Unfortunately, the documentation on CallbackValidatorAttribute is severely lacking.  Here’s what I found out using Reflector:

  • Both the CallbackMethodName and Type properties of the CallbackValidatorAttribute must be specified.  The Type property is the type of object that contains the CallbackMethodName method.
  • The callback method must be public, static and be a ValidatorCallback delegate.
  • To fail validation, throw an ArgumentException during execution of the callback.  Reflector isn’t actually any help here for the CallbackValidator, but this is what the other validator types do.

I’ve created a small sample demonstrating the use of the CallbackValidatorAttribute.  You can get it here.  The sample validator itself isn’t very useful, but hopefully it illustrates the concept well enough.

Posted on Friday, October 31, 2008 5:01 PM .NET Programming | Back to top

Comments on this post: Custom Config Sections and CallbackValidatorAttribute

Comments are closed.
Comments have been closed on this topic.
Copyright © Sean Carpenter | Powered by: