| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Reflection; |
| | 8 | |
|
| | 9 | | namespace System |
| | 10 | | { |
| | 11 | | public static class TypeExtensions |
| | 12 | | { |
| 17762 | 13 | | public static bool CanBeNull(this Type type) => type.IsReferenceType() || type.IsNullable(); |
| | 14 | |
|
| | 15 | | public static Type GetIEnumerable(this Type type) => |
| 7998 | 16 | | type.IsGenericEnumerable() ? type : type.GetTypeInfo().ImplementedInterfaces.FirstOrDefault(IsGenericEnumera |
| | 17 | |
|
| | 18 | | public static bool ImplementsGenericEquatable(this Type type) => |
| 14772 | 19 | | type.IsGenericEquatable() || type.GetTypeInfo().ImplementedInterfaces.Any(IsGenericEquatable); |
| | 20 | |
|
| | 21 | | public static bool IsIEnumerable(this Type type) => |
| 696 | 22 | | type.IsGenericEnumerable() || type.GetTypeInfo().ImplementedInterfaces.Any(IsGenericEnumerable); |
| | 23 | |
|
| | 24 | | public static bool IsInteger(this Type type) => |
| 16 | 25 | | type == typeof(byte) || type == typeof(short) || type == typeof(int) || type == typeof(long); |
| | 26 | |
|
| 9560 | 27 | | public static bool IsNullable(this Type type) => type.IsGenericWithDefinition(typeof(Nullable<>)); |
| | 28 | |
|
| 18466 | 29 | | public static bool IsReferenceType(this Type type) => !type.GetTypeInfo().IsValueType; |
| | 30 | |
|
| 14734 | 31 | | private static bool IsGenericEnumerable(this Type type) => type.IsGenericWithDefinition(typeof(IEnumerable<>)); |
| | 32 | |
|
| 48346 | 33 | | private static bool IsGenericEquatable(this Type type) => type.IsGenericWithDefinition(typeof(IEquatable<>)); |
| | 34 | |
|
| | 35 | | private static bool IsGenericWithDefinition(this Type type, Type genericTypeDef) => |
| 72640 | 36 | | type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == genericTypeDef; |
| | 37 | | } |
| | 38 | | } |