对不起标题不清楚,我希望有人能澄清。
我希望创建一个泛型接口,其中需要让泛型类型T从基类继承,但是基类也是泛型类型:Base<U>。是否有一种方法可以指定T需要从Base继承而不需要指定U?
示例:
public interface ICommentRepository<T> : IBaseRepository<T>
where T : Comment<U>, new()我不关心U所以我不想要ICommentRepository<T, U>。这也会使实现变得尴尬:
public class ArticleCommentRepository : ICommentRepository<Comment<Article>, Article> { .. }必须提供两次Article。
发布于 2020-02-12 11:35:49
这取决于你为什么需要U?如果您不需要它作为约束,您可以很容易地创建非泛型基,并将其用作约束。
public class Base
{
}
public class Base<T> : Base
{
}
public interface ICommentRepository<T> : IBaseRepository<T>
where T : Base, new()
{
}如果泛型约束并不重要,但仍然需要使用泛型基约束,甚至可以这样做。
public interface ICommentRepository<T> : IBaseRepository<T>
where T : Base<object>, new()
{
}如果您能够发布更多关于Base<T>的详细信息以及为什么它是通用的,它可以帮助我们解决您的问题。
干杯!
https://stackoverflow.com/questions/60151090
复制相似问题