5 using System.Reflection;
6 using System.Runtime.CompilerServices;
8 namespace SiliconStudio.Core.Reflection
12 public static bool HasInterface(
this Type type, Type lookInterfaceType)
14 return type.GetInterface(lookInterfaceType) != null;
17 public static Type
GetInterface(
this Type type, Type lookInterfaceType)
20 throw new ArgumentNullException(
"type");
21 if (lookInterfaceType == null)
22 throw new ArgumentNullException(
"lookInterfaceType");
24 var typeinfo = lookInterfaceType.GetTypeInfo();
25 if (typeinfo .IsGenericTypeDefinition)
27 if (typeinfo.IsInterface)
28 foreach (var interfaceType
in type.GetTypeInfo().ImplementedInterfaces)
29 if (interfaceType.GetTypeInfo().IsGenericType
30 && interfaceType.GetGenericTypeDefinition() == lookInterfaceType)
33 for (Type t = type; t != null; t = t.GetTypeInfo().BaseType)
34 if (t.GetTypeInfo().IsGenericType && t.GetGenericTypeDefinition() == lookInterfaceType)
39 if (lookInterfaceType.GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
40 return lookInterfaceType;
52 internal static string GetShortAssemblyQualifiedName(
this Type type)
54 var typeName = type.AssemblyQualifiedName;
57 throw new InvalidOperationException(
"Unable to get an assembly qualified name for type [{0}]".ToFormat(type));
60 var indexAfterType = typeName.IndexOf(
',');
61 if (indexAfterType >= 0)
63 var indexAfterAssembly = typeName.IndexOf(
',', indexAfterType + 1);
64 if (indexAfterAssembly >= 0)
66 typeName = typeName.Substring(0, indexAfterAssembly).Replace(
" ",
string.Empty);
84 return a.Equals(
b) || b.Equals(a);
97 return type.GetTypeInfo().GetCustomAttributes(typeof(CompilerGeneratedAttribute),
false).
Any()
98 && type.Namespace == null
99 && type.FullName.Contains(
"AnonymousType");
109 return Nullable.GetUnderlyingType(type) != null;
121 if (type == typeof(IntPtr))
123 if (type.GetTypeInfo().IsPrimitive)
125 if (type.GetTypeInfo().IsEnum)
127 if (!type.GetTypeInfo().IsValueType)
130 return type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).All(f => IsPureValueType(f.FieldType));
140 return type != null && type.GetTypeInfo().IsValueType && !type.GetTypeInfo().IsPrimitive && !type.GetTypeInfo().IsEnum;
150 return type != null && (type == typeof(sbyte) || type == typeof(
short) || type == typeof(
int) || type == typeof(
long) ||
151 type == typeof(byte) || type == typeof(ushort) || type == typeof(uint) || type == typeof(ulong) ||
152 type == typeof(
float) || type == typeof(
double) || type == typeof(decimal));
164 if (type == typeof(sbyte))
165 return sbyte.MinValue;
166 if (type == typeof(
short))
167 return short.MinValue;
168 if (type == typeof(
int))
170 if (type == typeof(
long))
171 return long.MinValue;
172 if (type == typeof(byte))
173 return byte.MinValue;
174 if (type == typeof(ushort))
175 return ushort.MinValue;
176 if (type == typeof(uint))
177 return uint.MinValue;
178 if (type == typeof(ulong))
179 return ulong.MinValue;
180 if (type == typeof(
float))
181 return float.MinValue;
182 if (type == typeof(
double))
183 return double.MinValue;
184 if (type == typeof(decimal))
185 return decimal.MinValue;
187 throw new ArgumentException(
"Numeric type expected");
199 if (type == typeof(sbyte))
200 return sbyte.MaxValue;
201 if (type == typeof(
short))
202 return short.MaxValue;
203 if (type == typeof(
int))
205 if (type == typeof(
long))
206 return long.MaxValue;
207 if (type == typeof(byte))
208 return byte.MaxValue;
209 if (type == typeof(ushort))
210 return ushort.MaxValue;
211 if (type == typeof(uint))
212 return uint.MaxValue;
213 if (type == typeof(ulong))
214 return ulong.MaxValue;
215 if (type == typeof(
float))
216 return float.MaxValue;
217 if (type == typeof(
double))
218 return double.MaxValue;
219 if (type == typeof(decimal))
220 return decimal.MaxValue;
222 throw new ArgumentException(
"Numeric type expected");
233 var doubleValue = CastToDouble(obj);
234 if (
double.IsNaN(doubleValue))
237 if (obj is decimal && type == typeof(decimal))
240 object result = null;
241 if (type == typeof(sbyte))
242 result = (sbyte)doubleValue;
243 if (type == typeof(byte))
244 result = (byte)doubleValue;
245 if (type == typeof(
short))
246 result = (
short)doubleValue;
247 if (type == typeof(ushort))
248 result = (ushort)doubleValue;
249 if (type == typeof(
int))
250 result = (
int)doubleValue;
251 if (type == typeof(uint))
252 result = (uint)doubleValue;
253 if (type == typeof(
long))
254 result = (
long)doubleValue;
255 if (type == typeof(ulong))
256 result = (ulong)doubleValue;
257 if (type == typeof(
float))
258 result = (
float)doubleValue;
259 if (type == typeof(
double))
260 result = doubleValue;
261 if (type == typeof(decimal))
262 result = (decimal)doubleValue;
271 internal static double CastToDouble(
object obj)
273 var result = double.NaN;
274 var type = obj != null ? obj.GetType() : null;
275 if (type == typeof(sbyte))
277 if (type == typeof(byte))
279 if (type == typeof(
short))
281 if (type == typeof(ushort))
282 result = (ushort)obj;
283 if (type == typeof(
int))
285 if (type == typeof(uint))
287 if (type == typeof(
long))
289 if (type == typeof(ulong))
291 if (type == typeof(
float))
293 if (type == typeof(
double))
294 result = (
double)obj;
295 if (type == typeof(decimal))
296 result = (
double)(decimal)obj;
static bool AreEqual(object a, object b)
Compare two objects to see if they are equal or not. Null is acceptable.
static bool IsAnonymous(this Type type)
Determines whether the specified type is an anonymous type.
static object GetMinimum(this Type type)
Gets the minimum value for the given numeric type.
static bool IsNumeric(this Type type)
Indicates whether the given type represents a numeric value.
static bool IsStruct(this Type type)
Returnes true if the specified type is a struct type.
Let the emitter choose the style.
static object GetMaximum(this Type type)
Gets the maximum value for the given numeric type.
static object CastToNumericType(this Type type, object obj)
Cast an object to a specified numeric type.
static Type GetInterface(this Type type, Type lookInterfaceType)
static bool HasInterface(this Type type, Type lookInterfaceType)
static bool IsPureValueType(this Type type)
Check if the type is a ValueType and does not contain any non ValueType members.
static bool IsNullable(this Type type)
Determines whether the specified type is nullable Nullable{T}.