// Copyright © 2010-2017 The CefSharp Authors. All rights reserved. // // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace CefSharp.ModelBinding { /// /// Containing extensions for the object. /// internal static class ModelBindingExtensions { /// /// Creates an instance of and cast it to . /// /// The type to create an instance of. /// if a non-public constructor can be used, otherwise . public static T CreateInstance(this Type type, bool nonPublic = false) { if (!typeof(T).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo())) { throw new InvalidOperationException("Unable to create instance of " + type.GetTypeInfo().FullName + "since it can't be cast to " + typeof(T).GetTypeInfo().FullName); } return (T)CreateInstance(type, nonPublic); } /// /// Creates an instance of . /// /// The type to create an instance of. /// if a non-public constructor can be used, otherwise . public static object CreateInstance(this Type type, bool nonPublic = false) { return CreateInstanceInternal(type, nonPublic); } /// /// returns the assembly that the type belongs to /// /// /// The assembly that contains the type public static Assembly GetAssembly(this Type source) { return source.GetTypeInfo().Assembly; } /// /// Checks if a type is an array or not /// /// The type to check. /// if the type is an array, otherwise . public static bool IsArray(this Type source) { return source.GetTypeInfo().BaseType == typeof(Array); } /// /// Is the type a collection, array or enumerable /// /// source type /// return if collection, array or enumerable public static bool IsCollectionOrArray(this Type source) { return source.IsCollection() || source.IsArray() || source.IsEnumerable(); } /// /// Determines whether the is assignable from /// taking into account generic definitions /// /// /// Borrowed from: http://tmont.com/blargh/2011/3/determining-if-an-open-generic-type-isassignablefrom-a-type /// public static bool IsAssignableToGenericType(this Type givenType, Type genericType) { if (givenType == null || genericType == null) { return false; } return givenType == genericType || givenType.MapsToGenericTypeDefinition(genericType) || givenType.HasInterfaceThatMapsToGenericTypeDefinition(genericType) || givenType.GetTypeInfo().BaseType.IsAssignableToGenericType(genericType); } /// /// Checks if a type is an collection or not /// /// The type to check. /// if the type is an collection, otherwise . public static bool IsCollection(this Type source) { var collectionType = typeof(ICollection<>); return source.GetTypeInfo().IsGenericType && source .GetInterfaces() .Any(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == collectionType); } /// /// Checks if a type is enumerable or not /// /// The type to check. /// if the type is an enumerable, otherwise . public static bool IsEnumerable(this Type source) { var enumerableType = typeof(IEnumerable<>); return source.GetTypeInfo().IsGenericType && source.GetGenericTypeDefinition() == enumerableType; } /// /// Determines if a type is numeric. Nullable numeric types are considered numeric. /// /// /// Boolean is not considered numeric. /// public static bool IsNumeric(this Type source) { if (source == null) { return false; } var underlyingType = Nullable.GetUnderlyingType(source) ?? source; if (underlyingType.GetTypeInfo().IsEnum) { return false; } switch (underlyingType.GetTypeCode()) { case TypeCode.Byte: case TypeCode.Decimal: case TypeCode.Double: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: case TypeCode.SByte: case TypeCode.Single: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: return true; default: return false; } } /// /// Filters our all types not assignable to . /// /// The type that all resulting should be assignable to. /// An of instances that should be filtered. /// An of instances. public static IEnumerable NotOfType(this IEnumerable types) { return types.Where(t => !typeof(TType).IsAssignableFrom(t)); } private static bool HasInterfaceThatMapsToGenericTypeDefinition(this Type givenType, Type genericType) { return givenType .GetInterfaces() .Where(it => it.GetTypeInfo().IsGenericType) .Any(it => it.GetGenericTypeDefinition() == genericType); } private static bool MapsToGenericTypeDefinition(this Type givenType, Type genericType) { return genericType.GetTypeInfo().IsGenericTypeDefinition && givenType.GetTypeInfo().IsGenericType && givenType.GetGenericTypeDefinition() == genericType; } public static TypeCode GetTypeCode(this Type type) { if (type == typeof(bool)) return TypeCode.Boolean; else if (type == typeof(char)) return TypeCode.Char; else if (type == typeof(sbyte)) return TypeCode.SByte; else if (type == typeof(byte)) return TypeCode.Byte; else if (type == typeof(short)) return TypeCode.Int16; else if (type == typeof(ushort)) return TypeCode.UInt16; else if (type == typeof(int)) return TypeCode.Int32; else if (type == typeof(uint)) return TypeCode.UInt32; else if (type == typeof(long)) return TypeCode.Int64; else if (type == typeof(ulong)) return TypeCode.UInt64; else if (type == typeof(float)) return TypeCode.Single; else if (type == typeof(double)) return TypeCode.Double; else if (type == typeof(decimal)) return TypeCode.Decimal; else if (type == typeof(DateTime)) return TypeCode.DateTime; else if (type == typeof(string)) return TypeCode.String; else if (type.GetTypeInfo().IsEnum) return GetTypeCode(Enum.GetUnderlyingType(type)); else return TypeCode.Object; } private static object CreateInstanceInternal(Type type, bool nonPublic = false) { var constructor = type.GetDefaultConstructor(nonPublic); if (constructor == null) { throw new MissingMethodException("No parameterless constructor defined for this object."); } return constructor.Invoke(new object[0]); } private static ConstructorInfo GetDefaultConstructor(this Type type, bool nonPublic = false) { var typeInfo = type.GetTypeInfo(); var constructors = typeInfo.DeclaredConstructors; foreach (var constructor in constructors) { var parameters = constructor.GetParameters(); if (parameters.Length > 0) { continue; } if (!constructor.IsPrivate || nonPublic) { return constructor; } } return null; } } }