Question:
Is there a work around to be able to use generics in the constructor of a class that inherits from a class that doesn't have generic types defined?
Explanation:
I am creating a new custom attribute inheriting from the ValidationAttribute class. I want to use generic types in the constructor as seen below:
DateValidationAttribute : ValidationAttribute {
public void DateValidationAttribute <TData>(Expression<Func<TData, DateTime?>> property){ ... }
}
However, this doesn't seem to be allowed as the TData and TProperty generic types aren't recognized as generics. In addition I cannot define the generics in the class definition as it inherits from the ValidationAttribute class. The error I get is that TData and TProperty is not defined.
Goal:
The goal of passing and expression down to have a strongly typed way of passing attribute names. I am writing this attribute to compare two properties of a class. The implementation below is what it looks like. Beneath that is how I want the implementation to look like:
Current:
public DateTime? ToDate { get; set; }
[DateValidation("ToDate")]
public DateTime? FromDate { get; set; }
Goal:
public DateTime? ToDate { get; set; }
[DateValidation(prop => prop.ToDate)]
public DateTime? FromDate { get; set; }