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     }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM