views:

46

answers:

2

I'm writing a PropertiesMustMatch validation attribute that can take a string property name as a parameter. I'd like it to find the corresponding property by name on that object and do a basic equality comparison. What's the best way to access this through reflection?

Also, I checked out the Validation application block in the Enterprise Library and decided its PropertyComparisonValidator was way too intense for what we need.

UPDATE: For further clarification (to provide some context), the goal is simply validation that enforces field matching (e.g., password verification). We'd like it to work with property-level attribute data annotations that inherit from the ValidationAttribute class, if possible.

UPDATE: In case anyone is curious, I ended up solving the actual business problem through tweaking code provided as an answer to this question

+1  A: 

You cannot do that. See also this question. Try to change the logic to work with the object, checking its attributes, not vice versa. You can also provide more information about your task, not just this narrow question.

wRAR
Please see my clarification update
Brandon Linton
+2  A: 

You can't, basically. The code that checks the object for the presence of the attribute must also assume responsibility for telling any code which type/object it was looking at. You can't obtain any additional metadata from within an attribute.

Marc Gravell
Is there a best-case workaround for this? It appears, sadly, it isn't as easy as something like this: `[MustMatchValidator(this, "FieldToMatch")]`
Brandon Linton
@Brandon: nope.
Marc Gravell
@Marc - thanks for the info
Brandon Linton