Is it possible to use the standard data annotations within the .NET framework (such as StringLength, RegularExpression, Required, etc.) with custom types?
What I want to do is something like this:
class MyProperty<TValue>
{
public bool Reset { get; set; }
public TValue Value { get; set; }
}
class MyClass
{
[Required]
[StringLength(32)]
MyProperty<string> Name { get; set; }
[StringLength(128)]
MyProperty<string> Address { get; set; }
}
And, of course, I want the validation to act upon the "Value" property. Looking at the code of the various validation attributes it looked like they were simply calling ToString() to get a value from the object. I tried overriding ToString() and return Value as a string, but excpetions are being thrown stating that the annotation cannot cast the object (Value) to string (even though the override did just that...?).
I'm trying to avoid writing custom versions of all the various possible validators to accommodate this simple type.