Limit the use of attribute validation (ValidationAttribute) to only one type of property

The question arises: how can I limit my validation attribute to be used for only one type ?, For example, only DateTime.

I am currently using this "IsValid" control method:

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 determine the type of attribute to which it is applied, or an attribute, for example:

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

where it restricts the use of my attribute properties only.

Thank.

+3
source share

All Articles