| | | 1 | | // ReSharper disable once CheckNamespace |
| | | 2 | | namespace Elsa.Extensions; |
| | | 3 | | |
| | | 4 | | /// <summary> |
| | | 5 | | /// Extends <see cref="Type"/> with additional methods. |
| | | 6 | | /// </summary> |
| | | 7 | | public static class TypeExtensions |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Returns true of the type is generic, false otherwise. |
| | | 11 | | /// </summary> |
| | 328 | 12 | | public static bool IsGenericType(this Type type, Type genericType) => type.IsGenericType && type.GetGenericTypeDefin |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Returns true of the type is nullable, false otherwise. |
| | | 16 | | /// </summary> |
| | 328 | 17 | | public static bool IsNullableType(this Type type) => type.IsGenericType(typeof(Nullable<>)); |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Returns the wrapped type of the specified nullable type. |
| | | 21 | | /// </summary> |
| | 12 | 22 | | public static Type GetTypeOfNullable(this Type type) => type.GenericTypeArguments[0]; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Returns true if the specified type is a collection type, false otherwise. |
| | | 26 | | /// </summary> |
| | | 27 | | public static bool IsCollectionType(this Type type) |
| | | 28 | | { |
| | 0 | 29 | | if (!type.IsGenericType) |
| | 0 | 30 | | return false; |
| | | 31 | | |
| | 0 | 32 | | var elementType = type.GenericTypeArguments[0]; |
| | 0 | 33 | | var collectionType = typeof(ICollection<>).MakeGenericType(elementType); |
| | 0 | 34 | | var listType = typeof(IList<>).MakeGenericType(elementType); |
| | 0 | 35 | | return collectionType.IsAssignableFrom(type) || listType.IsAssignableFrom(type); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Constructs a collection type from the specified type. |
| | | 40 | | /// </summary> |
| | 0 | 41 | | public static Type MakeCollectionType(this Type type) => typeof(ICollection<>).MakeGenericType(type); |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Returns the element type of the specified collection type. |
| | | 45 | | /// </summary> |
| | 0 | 46 | | public static Type GetCollectionElementType(this Type type) => type.GenericTypeArguments[0]; |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Determines whether the specified type is a numeric type. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="type">The type to check.</param> |
| | | 52 | | /// <returns>True if the specified type is numeric, otherwise false.</returns> |
| | | 53 | | public static bool IsNumericType(this Type type) |
| | | 54 | | { |
| | 18 | 55 | | return type.IsPrimitive || type == typeof(decimal) || type == typeof(float) || type == typeof(double) || type == |
| | | 56 | | } |
| | | 57 | | } |