不可能的递归泛型类定义?

Mat*_*son 9 c#

挑战: 请创建以下类的实例(使用任何类型为T):

class Foo<T>
    where T : Foo<T>
{
}
Run Code Online (Sandbox Code Playgroud)

你可以使用任何你喜欢的技术; 简单的"新MyClass ......",使用反射,黑客攻击MSIL,等等.

Mar*_*ell 12

static class Program {
    static void Main() {
        Foo<Bar> foo = new Foo<Bar>();
    }
}
class Foo<T> where T : Foo<T> {}
class Bar : Foo<Bar> {}
Run Code Online (Sandbox Code Playgroud)

  • 或者只是`new Bar()`. (3认同)