This is actually a subset of the question of how to get an instance decorated with an attribute from an attribute (similar question here ).
Unfortunately, the short answer is: you cannot.
Attributes are metadata. The attribute does not know and does not know anything about the class or member that it adorns. Some downstream class consumers should look for the specified user attributes and decide when / when / how to apply them.
You should think of attributes as data, not objects. Although attributes are technical classes, they are pretty dumb because they have a crucial limitation: everything about them needs to be determined at compile time. This actually means that they cannot access any runtime information unless they expose a method that accepts an instance of the runtime and the caller decides to call it.
You can do the last. You can create your own attribute, and as long as you control the validator, you can force the validator to call some method on the attribute and do something almost anything:
public abstract class CustomValidationAttribute : Attribute {
As long as someone uses this class, it uses the attribute correctly, this will work:
public class MyValidator { public IEnumerable<string> Validate(object instance) { if (instance == null) throw new ArgumentNullException("instance"); Type t = instance.GetType(); var validationAttributes = (CustomValidationAttribute[])Attribute .GetCustomAttributes(t, typeof(CustomValidationAttribute)); foreach (var validationAttribute in validationAttributes) { string error = validationAttribute.Validate(instance); if (!string.IsNullOrEmpty(error)) yield return error; } } }
If this is how you consume attributes, it becomes easier to implement your own:
public class PasswordValidationAttribute : CustomValidationAttribute { public override string Validate(object instance) { ChangePasswordModel model = instance as ChangePasswordModel; if (model == null) return null; if (model.NewPassword != model.ConfirmPassword) return Resources.GetLocalized("PasswordsDoNotMatch"); return null; } }
All this is fine and good, just the control flow is inverted from what you specified in the original question. The attribute knows nothing about what it was applied to; a validator that uses this attribute should provide this information (which can easily be done).
Of course, this is not the way validation really works with data annotations in MVC 2 (unless it has changed significantly since the last time I looked at it). I donβt think you can just plug this in using ValidationMessageFor and other similar features. But hey, in MVC 1, we still had to write all of our validators. There is nothing stopping you from combining DataAnnotations with your own validation attributes and validators; it just requires a bit more code. You will need to use a special validator wherever you write the verification code.
This is probably not the answer you are looking for, but unfortunately it is. the validation attribute cannot know about the class to which it was applied if this information is not provided by the validator itself.