确定列表的类型? [英] Determine type of a List?

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

问题描述

在c#中,我们可以在执行其他操作之前确定List持有的类型吗?示例:

In c#, can we determine what type a List is holding before doing something else? Example:

List<int> listing = new List<int>();

if(listing is int)
{
    // if List use <int> type, do this...
}
else if(listing is string)
{
    // if List use <string> type, do this...
}

推荐答案

您可以使用 Type.GetGenericArguments() 方法.

You can use Type.GetGenericArguments() method.

赞:

Type[] types = list.GetType().GetGenericArguments();
if (types.Length == 1 && types[0] == typeof(int))
{
    ...
}

这篇关于确定列表的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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