3

the question is this: How I can I restrict my validation attribute to be used for only one type?, For example only DateTime.

Currently I do this control method "IsValid":

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
    if (value == null || value.GetType() != typeof(DateTime))
    {
        return ValidationResult.Success;
    }
    ...
}

I would like this in the constructor, but I do not know how to detect the type of the attribute to which it applies, or an attribute such as:

[AttributeUsage(AttributeTargets.Property, ...)]
public class MyValidateDatesAttibute : ValidationAttribute, IClientValidatable
{
   ...
}

where it limits the use of my only attribute properties.

Thank you.

andres descalzo
  • 14,887
  • 13
  • 64
  • 115
  • I am afraid you can not limit usage to this level of granularity. You can still what is applied to run-time. And throw an exception or swallow it. – Val Bakhtin May 17 '12 at 18:07
  • You could use generics where T : DateTime but that would be lame since it's only for one type. Your other option is to throw a NotSupportedException when the type isn't supported by the method. – Guillaume Cantin May 18 '12 at 00:19

0 Answers0