C# 包含另一泛型类型的子级的泛型类

C# 包含另一泛型类型的子级的泛型类,c#,generics,C#,Generics,标题可能有点混乱,但我不确定如何用不同的措辞 我有一个泛型类型的类。我希望该类包含同一类的子类,但具有另一个泛型类型。大概是这样的: public class Test<Foo> { private readonly Foo _myFoo; public Test<ChildFoo> Child { get; set; } public Test(Foo foo) { _myFoo = foo; } } pub

标题可能有点混乱,但我不确定如何用不同的措辞

我有一个泛型类型的类。我希望该类包含同一类的子类,但具有另一个泛型类型。大概是这样的:

public class Test<Foo>
{
    private readonly Foo _myFoo;

    public Test<ChildFoo> Child { get; set; }

    public Test(Foo foo)
    {
        _myFoo = foo;
    }
}

public class Impl
{
    public void FooTest()
    {
        var parent = new Test<string>("tester");
        var child = new Test<int>(1234);

        parent.Child = child;
    }
}
公共类测试
{
私有只读foou myFoo;
公共测试子项{get;set;}
公开考试(Foo-Foo)
{
_myFoo=foo;
}
}
公共类Impl
{
公共空间
{
var父项=新测试(“测试人员”);
var child=新测试(1234);
parent.Child=Child;
}
}
但我不能生一个有“ChildFoo”的孩子。还有其他方法吗?

像这样试试

public class Test<T1, T2>
{
    private readonly T1 _myFoo;

    public T2 Child { get; set; }

    public Test(T1 foo)
    {
        _myFoo = foo;
    }
}

public class Impl
{
    public void FooTest()
    {
        var parent = new Test<string, Test<int, object>>("tester");
        var child = new Test<int, object>(1234);
        parent.Child = child;
    }
}
公共类测试
{
私有只读T1_myFoo;
公共T2子{get;set;}
公开考试(T1 foo)
{
_myFoo=foo;
}
}
公共类Impl
{
公共空间
{
var父项=新测试(“测试人员”);
var child=新测试(1234);
parent.Child=Child;
}
}
由于第一个解决方案不能满足您的需求,我还有一个涉及接口的想法,让您像对待
测试一样对待孩子

公共类测试:ITest其中T2:ITest
{
私有只读T1_myFoo;
公共T2子{get;set;}
公共图书馆A()
{
}
公共空间B()
{
Child.A();
}
公开考试(T1 foo)
{
_myFoo=foo;
}
}
公共接口测试
{
使A()无效;
无效B();
}
公共类Impl
{
公共空间
{
var父项=新测试(“测试人员”);
var child=新测试(1234);
parent.Child=Child;
}
}

我会尝试以下方法:

public class Test<T>
{
    private readonly T _myFoo;

    public Test(T foo)
    {
        _myFoo = foo;
    }
}

public class ParentTest<T, TChild, TChildType> : Test<T> where TChild : Test<TChildType>
{
    TChild Child { get; set; }
}

public class Impl
{
    public void FooTest()
    {
        var parent = new ParentTest<string, Test<int>, int>("tester");
        var child = new Test<int>(1234);

        parent.Child = child;
     }
}
公共类测试
{
私人只读T_myFoo;
公开考试(T-foo)
{
_myFoo=foo;
}
}
公共类ParentTest:Test,其中TChild:Test
{
TChild子项{get;set;}
}
公共类Impl
{
公共空间
{
var parent=新的ParentTest(“测试者”);
var child=新测试(1234);
parent.Child=Child;
}
}

这是正确的方法,只需对代码进行最小的修改

public class Test<Foo,ChildFoo>
{
    private readonly Foo _myFoo;

    public Test<ChildFoo,ChildFoo> Child { get; set; }

    public Test(Foo foo)
    {
        _myFoo = foo;
    }
}

public class Impl
{
    public void FooTest()
    {
        var parent = new Test<string,int>("tester");
        var child = new Test<int,int>(1234);

        parent.Child = child;
    }
}
公共类测试
{
私有只读foou myFoo;
公共测试子项{get;set;}
公开考试(Foo-Foo)
{
_myFoo=foo;
}
}
公共类Impl
{
公共空间
{
var父项=新测试(“测试人员”);
var child=新测试(1234);
parent.Child=Child;
}
}

Foo和ChildFoo之间有关系吗?@ZoharPeled没有。根据示例,Foo可以是字符串,ChildFoo可以是intfoo。这似乎是一个奇怪的设计。我怀疑这是一个错误,但我们缺乏确切的细节。你能分享更多关于这个类的实际用例的信息吗?我正在玩一个决策树。父节点将具有特定类型的输入,该输入将通过需求/谓词传递,如果成功,则通过其子节点传递。我真的不能提供任何代码,因为它还不存在,上面是我概念的初步证明。好吧,除了在
Test
中有两个类型参数外,我所知道的唯一两种方法是使用
对象作为
子对象的类型,或者使用非泛型基类或
Test
接口作为
child
的类型-两者没有太大区别,因为这将强制调用代码将
child
的值强制转换回原始类型(在您的示例中为
Test
),并且在过程中会丢失编译器类型安全性。这不会编译。测试不存在,现在它只是有意义的。什么是测试?它被称为链接。它可以包含像XML doesNB这样的递归元素:如果通过
ChildFoo
操作意味着它是一个派生类,那么您应该将上面的内容修改为:
public class Test,其中T2:T1
我也考虑过这样做。这是可行的,但这意味着我的父母需要知道孩子是什么类型的,这并不理想。@WynDiesel。它总是必须的。否则你怎么能用它呢?除非你准备在消费网站上播放。@WynDiesel我以适合你需要的方式编辑了它。
public class Test<Foo,ChildFoo>
{
    private readonly Foo _myFoo;

    public Test<ChildFoo,ChildFoo> Child { get; set; }

    public Test(Foo foo)
    {
        _myFoo = foo;
    }
}

public class Impl
{
    public void FooTest()
    {
        var parent = new Test<string,int>("tester");
        var child = new Test<int,int>(1234);

        parent.Child = child;
    }
}