我有以下类层次结构
public class EntityBase<T> where T : EntityBase<T>
{
//nothing interesting here
}
public class Benefit : EntityBase<Benefit>
{
//again, nothing interesting here
}
public class SeasonTicketLoan : Benefit
{
//nothing interesting here
}
现在我有了以下界面
public interface IQuery<T> where T : EntityBase<T>
{
}
当我尝试构建以下类时,我得到编译错误
public class EmployeesQuery : IQuery<SeasonTicketLoan>
{
}
我收到一条错误,指出SeasonTicketLoan
类不满足约束条件。
答案 0 :(得分:2)
Benefit
类也应该具有泛型类型 - 因此所有父类都将&#34; ultimate&#34; / sealed类型作为其泛型类型。只有&#34;终极&#34; /密封类型没有通用参数。
结果是在所有父类中,一直到根父类,泛型参数包含&#34;终极&#34; /密封类的类型,并且不会出现错误。
public class EntityBase<T> where T : EntityBase<T>
{
//nothing interesting here
}
public class Benefit<T> : EntityBase<T> where T : Benefit<T>
{
//again, nothing interesting here
}
public sealed class SeasonTicketLoan : Benefit<SeasonTicketLoan>
{
//nothing interesting here
}