加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 23:39:05 所属栏目:百科 来源:网络整理
导读:假设我有这样的清单: var firstList = new Listofsometype();var secondList = new Listofsomeanothertype();var thirdList = new Listanothertype(); 如何制作接受这些列表的列表?喜欢 var mainList = new List???();mainList.Add(firstlist);mainList.Ad
假设我有这样的清单:

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

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

var mainList = new List<???>();
mainList.Add(firstlist);
mainList.Add(secondlist);
mainList.Add(thirdlist);

谢谢.

解决方法

我可能会使用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);

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

>仅筛选某种类型的列表
>知道类型forList< object>稍后只需使用密钥即可

附:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读