C# - 获取通用列表的项类型 [英] C# - Get the item type for a generic list

查看:125
本文介绍了C# - 获取通用列表的项类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取通用列表包含的项目类型的最佳方式是什么?这很容易抓住集合中的第一个项目,并调用.GetType(),但我不能总是确定集合中会有一个项目。

What would be the best way of getting the type of items a generic list contains? It's easy enough to grab the first item in the collection and call .GetType(), but I can't always be sure there will be an item in the collection.

希望有意义。

感谢,

Sonny

Thanks,
Sonny

推荐答案

您可以使用 类型

You could use the Type.GetGenericArguments method for this purpose.

List<Foo> myList = ...

Type myListElementType = myList.GetType().GetGenericArguments().Single();

这篇关于C# - 获取通用列表的项类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆