Validation of Boxed Objects

Topics: Validation Application Block
Oct 17, 2008 at 4:27 PM
I'm trying to validate a boxed object. 
I have something like
A obj = new A();
object o = obj;

When I try this the validation rules never apply.  However when I try Validation.Validate(obj), it succeeds.  I need to be able to pass in boxed objects as I will be validating multiple objects whose types won't be known prior to validation.  Is there any way for me to do this?
Oct 19, 2008 at 2:11 PM
You could resort to creating a custom validator which would perform casting of objects to the type you want to validate.
Oct 24, 2008 at 11:25 AM

The Validation class is really a convenience facade which relies on generics to determine what to validate. In your case, because o has object as its static type the Validate method looks for the validation rules for the object class, not A's. To avoid this, ask the ValidationFactory for the validator for your type (see ValidationFactory.CreateValidator(Type) and use this validator to validate the object.

Hope this helps,
Oct 24, 2008 at 4:27 PM
Yes.  I recently came across that non-generic method and was able to implement it.  Thanks