0

I'm implementing a class which i want to inherit it from interface and class. It is working perfect when i use simple class but i want to make it generic. It is giving error when i made it generic. I'm sharing my code please guide me.

[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]


public abstract class ValidateCheckBox<TEntity, Validate, IValidate>
    where TEntity : class
    where Validate :
    ValidationAttribute
    where IValidate : IClientValidatable
{
    public int MinValue { get; set; }

    public ValidateCheckBox(int minValue)
    {
        MinValue = minValue;
        ErrorMessage = "At least " + MinValue + " {0} needs to be checked.";
    }

    public override string FormatErrorMessage(string propName)
    {
        return string.Format(ErrorMessage, propName);
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        try
        {
            List<CheckboxInfo> valueList = (List<CheckboxInfo>)value;
            foreach (var valueItem in valueList)
            {
                if (valueItem.Selected)
                {
                    return ValidationResult.Success;
                }
            }
            return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
        }
        catch (Exception x)
        {
            return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
        }
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
            ValidationType = "minchecked",
        };

        rule.ValidationParameters["minvalue"] = MinValue;

        yield return rule;
    }
}
DevWithSigns
  • 725
  • 16
  • 33

1 Answers1

1

The issue that is causing the error is that you are not inheriting from ValidationAttribute. You're using it as a constraint. See MSDN on constraints. But that brings up other problems. I don't think generic attributes are supported. See this and this.

I think the way you're going to have to use this is the plain ol':

public abstract class ValidateCheckBox : ValidationAttribute { ... }

It looks like there is a workaround by adding properties of Object to your custom attribute and specifying the generic type when using the attribute:

public class GenericClass<T>
{ ... }

public class CustomAttribute : Attribute
{
    public System.Object info;
}

And then using it like this:

[CustomAttribute(info = typeof(GenericClass<...>))]
...

More info about the workaround here.

Community
  • 1
  • 1
Neil Smith
  • 2,565
  • 1
  • 15
  • 18
  • Thanks for the code help, i have updated my question actually this is an attribute and i want to make it generic because it is just now working for "CheckboxInfo" class – DevWithSigns Jul 02 '14 at 20:59
  • 1
    @Usama As far as I'm aware, generic attributes aren't supported. See [this](http://stackoverflow.com/questions/294216/why-does-c-sharp-forbid-generic-attribute-types) and [this](http://stackoverflow.com/questions/9788593/workaround-for-c-sharp-generic-attribute-limitation). – Neil Smith Jul 02 '14 at 21:02
  • I hope you at least see that you aren't inheriting from ValidationAttribute. See [this](http://msdn.microsoft.com/en-us/library/d5x73970.aspx) for what you're actually doing. – Neil Smith Jul 02 '14 at 21:03