|
| 1 | +#if NET8_0 |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | +#endif |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using JetBrains.Annotations; |
| 7 | +using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; |
| 8 | + |
| 9 | +// ReSharper disable RedundantNullableFlowAttribute -- NotNull has an effect, see Issue72NotNullAttributeTests |
| 10 | + |
| 11 | + |
| 12 | +namespace Light.GuardClauses; |
| 13 | + |
| 14 | +public static partial class Check |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Checks if the specified type derives from the other type. Internally, this method uses <see cref="IsEquivalentTypeTo" /> |
| 18 | + /// by default so that constructed generic types and their corresponding generic type definitions are regarded as equal. |
| 19 | + /// </summary> |
| 20 | + /// <param name="type">The type info to be checked.</param> |
| 21 | + /// <param name="baseClass">The base class that <paramref name="type" /> should derive from.</param> |
| 22 | + /// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="baseClass" /> is null.</exception> |
| 23 | + [ContractAnnotation("type:null => halt; baseClass:null => halt")] |
| 24 | + public static bool DerivesFrom( |
| 25 | + [NotNull] [ValidatedNotNull] this Type type, |
| 26 | + [NotNull] [ValidatedNotNull] Type baseClass |
| 27 | + ) |
| 28 | + { |
| 29 | + baseClass.MustNotBeNull(nameof(baseClass)); |
| 30 | + |
| 31 | + var currentBaseType = type.MustNotBeNull(nameof(type)).BaseType; |
| 32 | + while (currentBaseType != null) |
| 33 | + { |
| 34 | + if (currentBaseType.IsEquivalentTypeTo(baseClass)) |
| 35 | + { |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + currentBaseType = currentBaseType.BaseType; |
| 40 | + } |
| 41 | + |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Checks if the specified type derives from the other type. This overload uses the specified <paramref name="typeComparer" /> |
| 47 | + /// to compare the types. |
| 48 | + /// </summary> |
| 49 | + /// <param name="type">The type info to be checked.</param> |
| 50 | + /// <param name="baseClass">The base class that <paramref name="type" /> should derive from.</param> |
| 51 | + /// <param name="typeComparer">The equality comparer used to compare the types.</param> |
| 52 | + /// <exception cref="ArgumentNullException">Thrown when <paramref name="type" />, or <paramref name="baseClass" />, or <paramref name="typeComparer" /> is null.</exception> |
| 53 | + [ContractAnnotation("type:null => halt; baseClass:null => halt; typeComparer:null => halt")] |
| 54 | + public static bool DerivesFrom( |
| 55 | + [NotNull] [ValidatedNotNull] this Type type, |
| 56 | + [NotNull] [ValidatedNotNull] Type baseClass, |
| 57 | + [NotNull] [ValidatedNotNull] IEqualityComparer<Type> typeComparer |
| 58 | + ) |
| 59 | + { |
| 60 | + baseClass.MustNotBeNull(nameof(baseClass)); |
| 61 | + typeComparer.MustNotBeNull(nameof(typeComparer)); |
| 62 | + |
| 63 | + var currentBaseType = type.MustNotBeNull(nameof(type)).BaseType; |
| 64 | + while (currentBaseType != null) |
| 65 | + { |
| 66 | + if (typeComparer.Equals(currentBaseType, baseClass)) |
| 67 | + { |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + currentBaseType = currentBaseType.BaseType; |
| 72 | + } |
| 73 | + |
| 74 | + return false; |
| 75 | + } |
| 76 | +} |
0 commit comments