C# 反射 判断类型是否是列表

复制代码
 1     /// <summary>
 2     /// 判断类型是否为可操作的列表类型
 3     /// </summary>
 4     /// <param name="type"></param>
 5     /// <returns></returns>
 6     public static bool IsList(this Type type)
 7     {
 8         if (typeof(System.Collections.IList).IsAssignableFrom(type))
 9         {
10             return true;
11         }
12         foreach (var it in type.GetInterfaces())
13         {
14             if (it.IsGenericType && typeof(IList<>) == it.GetGenericTypeDefinition())
15                 return true;
16         }
17         return false;
18     }
复制代码

 

复制代码
 1     /// <summary>
 2     /// 判断类型是否为列表类型
 3     /// </summary>
 4     /// <param name="type"></param>
 5     /// <returns></returns>
 6     public static bool IsEnumerable(this Type type)
 7     {
 8         if (type.IsArray)
 9         {
10             return true;
11         }
12         if (typeof(System.Collections.IEnumerable).IsAssignableFrom(type))
13         {
14             return true;
15         }
16         foreach (var it in type.GetInterfaces())
17             if (it.IsGenericType && typeof(IEnumerable<>) == it.GetGenericTypeDefinition())
18                 return true;
19         return false;
20     }
复制代码

 

posted @   唐宋元明清2188  阅读(2273)  评论(0编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示
剑桥
16:15发布
剑桥
16:15发布
7°
西南风
3级
空气质量
相对湿度
66%
今天
2°/10°
周三
2°/8°
周四
中雨
2°/7°