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 
128  public static bool HasProperty(this Type obj, string PropertyName)
129  {
130  return obj.GetProperty(PropertyName) != null;
131  }
132 
140  public static bool IsBuiltinType(this Type type)
141  {
142  return type.Namespace == "System";
143  }
144 
145 
152  public static string GetFullName(this Type t)
153  {
154  if (!t.IsGenericType)
155  return t.Name;
156  StringBuilder sb = new StringBuilder();
157 
158  sb.Append(t.Name.Substring(0, t.Name.LastIndexOf("`")));
159  sb.Append(t.GetGenericArguments().Aggregate("<",
160 
161  delegate(string aggregate, Type type)
162  {
163  return aggregate + (aggregate == "<" ? "" : ",") + GetFullName(type);
164  }
165  ));
166  sb.Append(">");
167 
168  return sb.ToString();
169  }
170 
171 
172  public static string GetDisplayName(this Type t)
173  {
174  if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>))
175  return string.Format("{0}?", GetDisplayName(t.GetGenericArguments()[0]));
176  if (t.IsGenericType)
177  return string.Format("{0}<{1}>",
178  t.Name.Remove(t.Name.IndexOf('`')),
179  string.Join(",", t.GetGenericArguments().Select(at => at.GetDisplayName())));
180  if (t.IsArray)
181  return string.Format("{0}[{1}]",
182  GetDisplayName(t.GetElementType()),
183  new string(',', t.GetArrayRank() - 1));
184  return t.Name;
185  }
186 }