In the specific case of ValidationAttribute
this is possible, but you should use another IsValid
overload, which has a context parameter. The context can be used to get the containing type, and also to get the name of the property to which the attribute applies.
protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var requiredAttribute = validationContext.ObjectType .GetPropery(validationContext.MemberName) .GetCustomAttributes(true).OfType<RequiredAttribute>().SingleOrDefault(); }
source share