【发布时间】:2020-04-16 00:31:06
【问题描述】:
我正在尝试在 C# 中执行以下操作。
public class Parent<T> where T : Parent<???>
{
public T Prop { get; set; }
}
public class Child : Parent<Child>
{
}
我该怎么做?
【问题讨论】:
-
你只需要
class Parent<T> where T : Parent<T>吗? -
在你尝试使用的时候,Parent的定义是不是不完整?
-
@Enigmativity 这是递归的,它不起作用。
-
@PrateekShrivastava 你建议如何重构代码?有什么解决方法吗?
-
@geeko - 不,它工作正常。它会编译并允许您按照您要求的方式定义
class Child : Parent<Child>。
标签: c# generics inheritance type-parameter