WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
TypeExtensions.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Reflection;
6 
10 public static class TypeExtensions
11 {
19  public static bool IsNumber(this Type t)
20  {
21  return t == typeof(sbyte)
22  || t == typeof(byte)
23  || t == typeof(short)
24  || t == typeof(ushort)
25  || t == typeof(int)
26  || t == typeof(uint)
27  || t == typeof(long)
28  || t == typeof(ulong)
29  || t == typeof(float)
30  || t == typeof(double)
31  || t == typeof(decimal);
32  }
33 
38  public static List<PropertyInfo> GetXMLSerializableProperties(this Type t)
39  {
40  return t.GetProperties().Where(PI => PI.IsXMLSerializeable()).ToList();
41  }
42 
43 
51  public static bool IsGenericList(this Type type)
52  {
53  if (type == null) return false;
54 
55  foreach (Type @interface in type.GetInterfaces())
56  {
57  if (@interface.IsGenericType)
58  {
59  if (@interface.GetGenericTypeDefinition() == typeof(ICollection<>))
60  {
61 
62  return true;
63  }
64  }
65  }
66  return false;
67  }
68 
69 
77  public static bool IsGenericDictionary(this Type type)
78  {
79  if (type == null) return false;
80 
81  foreach (Type @interface in type.GetInterfaces())
82  {
83  if (@interface.IsGenericType)
84  {
85  if (@interface.GetGenericTypeDefinition() == typeof(IDictionary<,>))
86  {
87 
88  return true;
89  }
90  }
91  }
92  return false;
93  }
94 
95 
101  public static Type[] GetGetGenericCollectionTypeArguments(this Type type)
102  {
103  if (type == null) return null;
104  if (!type.IsGenericList() && !type.IsGenericDictionary()) return null;
105 
106  foreach (Type @interface in type.GetInterfaces())
107  {
108  if (@interface.IsGenericType)
109  {
110  if (@interface.GetGenericTypeDefinition() == typeof(ICollection<>) | @interface.GetGenericTypeDefinition() == typeof(IDictionary<,>))
111  {
112 
113  return @interface.GetGenericArguments();
114  }
115  }
116  }
117  return null;
118  }
119 
120 
121 }