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

前端 未结 4 1427
天命终不由人
天命终不由人 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

    Did you mean a list with an instance of each derived type?

    Assembly.GetExecutingAssembly().GetTypes
        .Where(type => type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(GenericType<>))
        .ToList().
        .ForEach(type => list.Add(Activator.CreateInstance(type)));
    

提交回复
热议问题