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