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     }
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/kybs0/p/15112006.html