C#泛型类型中的"当前类型"占位符?

Mic*_*ael 1 .net c# generics

基本上,我想做的是:

public class MySpecialCollection<T>
    where T : ISomething { ... }

public interface ISomething
{
    public ISomething NextElement { get; }
    public ISomething PreviousElement { get; }
}

public class XSomething : ISomething { ... }

MySpecialCollection<XSomething> coll;
XSomething element = coll.GetElementByShoeSize(39);
XSomething nextElement = element.NextElement; // <-- line of interest
Run Code Online (Sandbox Code Playgroud)

...无需将nextElement转换为XSomething.有任何想法吗?我本来想要的东西......

public interface ISomething
{
    public SameType NextElement { get; }
    public SameType PreviousElement { get; }
}
Run Code Online (Sandbox Code Playgroud)

先感谢您!

Guf*_*ffa 10

使界面通用:

public class MySpecialCollection<T> where T : ISomething<T> {
  ...
}

public interface ISomething<T> {
  T NextElement { get; }
  T PreviousElement { get; }
}

public class XSomething : ISomething<XSomething> {
  ...
}
Run Code Online (Sandbox Code Playgroud)