How do I create a list of objects that inherit from the same generic class with varying types?

前端 未结 4 1437
天命终不由人
天命终不由人 2020-12-11 22:27

I\'ve spent a few hours on and off trying to find an answer to this, I\'m probably having trouble with wording the question correctly, which doesn\'t help. Essentially, I ha

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-11 23:06

    So, if I understand correctly, you want something like this:

    List
    

    Which can contain any instance of AType, BType, etc. You simply do this by making GenericType itself a subclass of GenericType:

    public class GenericType { }
    public class GenericType : GenericType { }
    public class AType : GenericType { }
    public class BType : GenericType { }
    

    You can then stick any instance into a List.

提交回复
热议问题