Validation of Boxed Objects

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

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?
Thanks
Oct 19, 2008 at 3: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 12:25 PM
Hi,

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,
Fernando
Oct 24, 2008 at 5:27 PM
Yes.  I recently came across that non-generic method and was able to implement it.  Thanks