[英]C# - Specifying type parameters of super-class in sub-classes?
我正在嘗試在 C# 中執行以下操作。
public class Parent<T> where T : Parent<???>
{
public T Prop { get; set; }
}
public class Child : Parent<Child>
{
}
我該怎么做?
這工作正常:
public class Parent<T> where T : Parent<T>
{
public T Prop { get; set; }
}
public class Child : Parent<Child>
{
}
請注意這一點,因為 c# 不會強制執行真正的Parent
/ Child
關系。 例如,給定上面的代碼,我也可以這樣做:
public class Stranger : Parent<Child>
{
}
如果您編寫單元測試,那么值得編寫一個類型檢查器來查找這種錯誤模式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.