不可能的递归泛型类定义吗? [英] Impossible recursive generic class definition?

查看:26
本文介绍了不可能的递归泛型类定义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Challenge: Please create an instance of the following class (using any type as T):

class Foo<T>
    where T : Foo<T>
{
}

您可以使用任何喜欢的技术;普通的"new MyClass ...",使用反射功能,入侵MSIL,等等.

You may use any technique you like; plain "new MyClass...", using reflection, hacking MSIL, whatever.

推荐答案

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

这篇关于不可能的递归泛型类定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆