2 using System.Collections.Generic;
5 using System.Reflection;
10 public static class TypeExtensions
19 public static bool IsNumber(
this Type t)
21 return t == typeof(sbyte)
24 || t == typeof(ushort)
30 || t == typeof(
double)
31 || t == typeof(decimal);
38 public static List<PropertyInfo> GetXMLSerializableProperties(
this Type t)
40 return t.GetProperties().Where(PI => PI.IsXMLSerializeable()).ToList();
51 public static bool IsGenericList(
this Type type)
53 if (type == null)
return false;
55 foreach (Type
@interface in type.GetInterfaces())
57 if (
@interface.IsGenericType)
59 if (
@interface.GetGenericTypeDefinition() == typeof(ICollection<>))
77 public static bool IsGenericDictionary(
this Type type)
79 if (type == null)
return false;
81 foreach (Type
@interface in type.GetInterfaces())
83 if (
@interface.IsGenericType)
85 if (
@interface.GetGenericTypeDefinition() == typeof(IDictionary<,>))
101 public static Type[] GetGetGenericCollectionTypeArguments(
this Type type)
103 if (type == null)
return null;
104 if (!type.IsGenericList() && !type.IsGenericDictionary())
return null;
106 foreach (Type
@interface in type.GetInterfaces())
108 if (
@interface.IsGenericType)
110 if (
@interface.GetGenericTypeDefinition() == typeof(ICollection<>) |
@interface.GetGenericTypeDefinition() == typeof(IDictionary<,>))
113 return @interface.GetGenericArguments();