如何创建包含不同类型列表的列表

jas*_*son 3 c#

假设我有这样的清单:

var firstList = new List<ofsometype>();
var secondList = new List<ofsomeanothertype>();
var thirdList = new List<anothertype>();
Run Code Online (Sandbox Code Playgroud)

如何制作接受这些列表的列表?喜欢

var mainList = new List<???>();
mainList.Add(firstlist);
mainList.Add(secondlist);
mainList.Add(thirdlist);
Run Code Online (Sandbox Code Playgroud)

谢谢.

Fab*_*jan 5

我可能会使用Dictionary集合:

var firstList = new List<ofsometype>();
var secondList = new List<ofsomeanothertype>();
var thirdlist = new List<anothertype>();

var listsDict = new Dictionary<Type, object>();
listsDict.Add(typeof(ofsometype), firstlist);
listsDict.Add(typeof(ofsomeanothertype), secondlist);
listsDict.Add(typeof(anothertype), thirdlist);
Run Code Online (Sandbox Code Playgroud)

这里的优点是它为您提供有关列表类型的信息.这可以用于两件事:

  1. 仅过滤特定类型的列表
  2. List<object>只需使用密钥即可知道以后的类型

PS

根据解决方案的内容和您需要实现的目标,您可以使用泛型(如果类型已知)或dynamics - 如果类型未知,但如果编译器不知道,则仍需要在运行时进行动态操作类型.