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 @ 2021-08-07 15:15  唐宋元明清2188  阅读(1683)  评论(0编辑  收藏  举报