With a bit of reflection, you could build your own validator which scans the ValidationAttributes
on the properties you have. It may not be a perfect solution, but if you're limited to using .NET 3.5, this seems like a lightweight solution, hopefully you get the picture.
static void Main(string[] args)
{
Person p = new Person();
p.Age = 4;
var results = Validator.Validate(p);
results.ToList().ForEach(error => Console.WriteLine(error));
Console.Read();
}
// Simple Validator class
public static class Validator
{
// This could return a ValidationResult object etc
public static IEnumerable<string> Validate(object o)
{
Type type = o.GetType();
PropertyInfo[] properties = type.GetProperties();
Type attrType = typeof (ValidationAttribute);
foreach (var propertyInfo in properties)
{
object[] customAttributes = propertyInfo.GetCustomAttributes(attrType, inherit: true);
foreach (var customAttribute in customAttributes)
{
var validationAttribute = (ValidationAttribute)customAttribute;
bool isValid = validationAttribute.IsValid(propertyInfo.GetValue(o, BindingFlags.GetProperty, null, null, null));
if (!isValid)
{
yield return validationAttribute.ErrorMessage;
}
}
}
}
}
public class Person
{
[Required(ErrorMessage = "Name is required!")]
public string Name { get; set; }
[Range(5, 20, ErrorMessage = "Must be between 5 and 20!")]
public int Age { get; set; }
}
This prints out the following to the Console:
Name is required!
Must be between 5 and 20!