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

C# 不可能的递归泛型类定义?,c#,C#,挑战: 请创建以下类的实例(使用任何类型作为T): class-Foo T:Foo在哪里 { } 你可以使用任何你喜欢的技巧;普通的“new MyClass…”,使用反射、黑客MSIL等等。静态类程序{ static class Program { static void Main() { Foo<Bar> foo = new Foo<Bar>(); } } class Foo<T> where T : Foo<T>

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

class-Foo
T:Foo在哪里
{
}
你可以使用任何你喜欢的技巧;普通的“new MyClass…”,使用反射、黑客MSIL等等。

静态类程序{
static class Program {
    static void Main() {
        Foo<Bar> foo = new Foo<Bar>();
    }
}
class Foo<T> where T : Foo<T> {}
class Bar : Foo<Bar> {}
静态void Main(){ Foo-Foo=新的Foo(); } } 类Foo,其中T:Foo{} 类栏:Foo{}
洞察你想要实现的目标可能有助于人们给出更好的备选答案。可能重复的“我”真的没有这个奇怪结构的目的。坦白地说,我认为这是不可能的,所以我得到了一个答案很酷。(而且,不,这不是一个家庭作业问题…;-)我真的有这个目的。一种具有自身集合的接口,例如,
接口IPerson,其中T:IPerson{IEnumerable substances}
。接口的实现者需要下属是一个具体类型的集合
T
,否则他们会在通用集合的协变/逆变方面遇到各种各样的问题。我数不清会让事情变得困难。哈哈,酷!我真的不认为这是可能的!呵呵。我被纠正了。真是太好了!:)
static class Program {
    static void Main() {
        Foo<Bar> foo = new Foo<Bar>();
    }
}
class Foo<T> where T : Foo<T> {}
class Bar : Foo<Bar> {}