I'm getting a compile-time error that my method call has some invalid arguments.
public abstract class EntityBase
{
public virtual List<ValidationResult> Validate<T>()
{
// This line causes the error:
var validationResults = this.ValidateEntity<T>(this, true).ToList();
}
protected IEnumerable<ValidationResult> ValidateEntity<T>(T entity, bool ignoreNullViolations)
{
// Code here
}
}
The class is abstract. That should be ok. I tried specifying the type of T in the method signature but that didn't help. Why won't this compile? I can't pass this
to a method expecting a T parameter?
EDIT -- Possible solution:
public virtual List<ValidationResult> Validate<T>() where T : class
{
var validationResults = this.ValidateEntity<T>(this as T, true).ToList();
}
EDIT 2
Since T should only be a subclass, I think the class signature should change to be generic so that forces the subclass to set it. Then passing this
wouldn't be such a hack.