代码之家  ›  专栏  ›  技术社区  ›  WynDiesel

包含其他泛型类型的子级的泛型类

  •  2
  • WynDiesel  · 技术社区  · 7 年前

    标题可能有点混乱,但我不确定如何用不同的方式表达。

    我有一个泛型类。我希望类包含同一类的子类,但使用另一个泛型类型。像这样:

    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;
        }
    }
    

    但我不能有一个“儿童食品”通用的孩子。还有别的办法吗?

    3 回复  |  直到 7 年前
        1
  •  4
  •   just-my-name    7 年前

    像这样试试。

    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;
        }
    }
    

    由于第一个解决方案不能满足您的需求,我还有一个涉及界面的想法,让我们让您像对待孩子一样对待它。 Test<,> .

    public class Test<T1, T2> : ITest where T2 : ITest
    {
        private readonly T1 _myFoo;
    
        public T2 Child { get; set; }
    
        public void A()
        {
        }
    
        public void B()
        {
            Child.A();
        }
    
        public Test(T1 foo)
        {
            _myFoo = foo;
        }
    }
    
    public interface ITest
    {
        void A();
        void B();
    }
    
    public class Impl
    {
        public void FooTest()
        {
            var parent = new Test<string, Test<int, ITest>>("tester");
            var child = new Test<int, ITest>(1234);
            parent.Child = child;
        }
    }
    
        2
  •  -1
  •   Yair Halberstadt    7 年前

    我会尝试这样的方法:

    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;
         }
    }
    
        3
  •  -1
  •   György Gulyás    7 年前

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

    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;
        }
    }