CustomValidationAttribute doesn't work when other attributes are applied to the class.
i reported on microsoft connect, closed won't fix, if have workarounds (other searching attributes manually), please let me know.
here problem:
imports system.componentmodel imports system.componentmodel.dataannotations module module1 sub main() dim type = gettype(contact) typedescriptor.addprovidertransparent( new associatedmetadatatypetypedescriptionprovider(type), type) dim contact new contact dim context new validationcontext(contact, nothing, nothing) dim errors new list(of validationresult) dim result = validator.tryvalidateobject(contact, context, errors, true) end sub end module <customvalidation(gettype(contact.contactmd), "*********************")> <metadatatype(gettype(contact.contactmd))> public class contact public property email string public property emailrepeat string public class contactmd '<required()> public property email '<required()> public property emailrepeat public shared function validateemails(byval contact contact) _ validationresult return if(contact.email = contact.emailrepeat, validationresult.success, new validationresult("fail!")) end function end class end class
the above code throw exception:
customvalidationattribute method '*********************' not exist in type 'contactmd' or not public , static.
this exception justified , it's sign things working.
once uncomment requiredattribute attributes on properties in metadata class, exception not thrown, means, validation system doesn't validate both property-typed attributes , class-level attributes.
any workarou
internally validation happens in several (currently 3) steps. if there errors found in 1 of steps, next steps not executed , errors returned immediatelly. looks design.
in case first step fails validate email , emailrepeat properties, because nothing - if pass 3rd argument (icollection<validationresult>), you will information. next steps of validation include class-level attributes not executed because of these validation errors first step. that's why not see exception.
-karel
.NET Framework > Common Language Runtime Internals and Architecture
Comments
Post a Comment