小编Mic*_*ael的帖子

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

基本上,我想做的是:

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)

先感谢您!

.net c# generics

1
推荐指数
1
解决办法
1681
查看次数

标签 统计

.net ×1

c# ×1

generics ×1