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.
Your
ValidateEntitymethod is declared such that the first parameter is of typeT.Now look how you’re calling it:
You’re trying to implicitly convert
thistoT– what makes you think that should work?Suppose I called:
That would try to pass
thisto a method effectively expecting astring– that’s clearly not going to work, asthisis a reference to an instance of some concrete subclass ofEntityBase– not a reference to a string.