C#-将“ this”作为类型参数[重复项]

问题描述 投票:0回答:1

我正在尝试在C#中执行以下操作:

public abstract class X
{
  Y<this> y { get; set; }
}

基本上是“ this”指向X的当前子类的类型。我该怎么办?

c# generics inheritance type-parameter
1个回答
3
投票
[X也必须是通用的-这是常见模式。

public abstract class X<T> where T : X<T> // ... public class Y : X<Y> // ...

您应该能够在运行时进行转换,并使用.GetType()对实例进行检查,以帮助做出明智的决定。

© www.soinside.com 2019 - 2024. All rights reserved.