gpt4 book ai didi

c# - 继承其泛型类型的泛型类

转载 作者:行者123 更新时间:2023-11-30 12:51:50 25 4
gpt4 key购买 nike

<分区>

Possible Duplicates:
Why cannot C# generics derive from one of the generic type parameters like they can in C++ templates?
In C# 4.0, is it possible to derive a class from a generic type parameter?

我正在尝试创建一个从其泛型类型继承的泛型类(在 1.Attempt 中)。看起来这是不可能的。但在其他尝试中,我必须转换对象。该问题的任何设计解决方案?

// #1 Attempt
public interface IFoo { }

public class Generic<T> : T { }

public void play ()
{
IFoo genfoo = new Generic<IFoo> ();
}

.

// #2 Attempt. Castable solution
public interface IAnything { }

public interface IFoo2 : IAnything { }

public class Generic2<T> : IAnything { }

public void play2 ()
{
IFoo2 genFoo = (IFoo2) new Generic2<IFoo2> ();
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com