22using System . Collections . Generic ;
33using System . Runtime . CompilerServices ;
44using JetBrains . Annotations ;
5+ #if NET8_0
6+ using System . Diagnostics . CodeAnalysis ;
7+ #endif
58
69namespace Light . GuardClauses ;
710
@@ -41,11 +44,19 @@ private static bool CheckTypeEquivalency(Type type, Type other)
4144 /// <param name="interfaceType">The interface type that <paramref name="type" /> should implement.</param>
4245 /// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="interfaceType" /> is null.</exception>
4346 [ ContractAnnotation ( "type:null => halt; interfaceType:null => halt" ) ]
44- public static bool Implements ( [ ValidatedNotNull ] this Type type , [ ValidatedNotNull ] Type interfaceType )
47+ public static bool Implements (
48+ #if NET8_0
49+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
50+ #endif
51+ [ ValidatedNotNull ]
52+ this Type type ,
53+ [ ValidatedNotNull ] Type interfaceType
54+ )
4555 {
46- interfaceType . MustNotBeNull ( nameof ( interfaceType ) ) ;
47- var implementedInterfaces = type . MustNotBeNull ( nameof ( type ) ) . GetInterfaces ( ) ;
56+ type . MustNotBeNull ( ) ;
57+ interfaceType . MustNotBeNull ( ) ;
4858
59+ var implementedInterfaces = type . GetInterfaces ( ) ;
4960 for ( var i = 0 ; i < implementedInterfaces . Length ; ++ i )
5061 {
5162 if ( interfaceType . IsEquivalentTypeTo ( implementedInterfaces [ i ] ) )
@@ -64,12 +75,21 @@ public static bool Implements([ValidatedNotNull] this Type type, [ValidatedNotNu
6475 /// <param name="typeComparer">The equality comparer used to compare the interface types.</param>
6576 /// <exception cref="ArgumentNullException">Thrown when <paramref name="type" />, or <paramref name="interfaceType" />, or <paramref name="typeComparer" /> is null.</exception>
6677 [ ContractAnnotation ( "type:null => halt; interfaceType:null => halt; typeComparer:null => halt" ) ]
67- public static bool Implements ( [ ValidatedNotNull ] this Type type , [ ValidatedNotNull ] Type interfaceType , [ ValidatedNotNull ] IEqualityComparer < Type > typeComparer )
78+ public static bool Implements (
79+ #if NET8_0
80+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
81+ #endif
82+ [ ValidatedNotNull ]
83+ this Type type ,
84+ [ ValidatedNotNull ] Type interfaceType ,
85+ [ ValidatedNotNull ] IEqualityComparer < Type > typeComparer
86+ )
6887 {
69- interfaceType . MustNotBeNull ( nameof ( interfaceType ) ) ;
70- typeComparer . MustNotBeNull ( nameof ( typeComparer ) ) ;
88+ type . MustNotBeNull ( ) ;
89+ interfaceType . MustNotBeNull ( ) ;
90+ typeComparer . MustNotBeNull ( ) ;
7191
72- var implementedInterfaces = type . MustNotBeNull ( nameof ( type ) ) . GetInterfaces ( ) ;
92+ var implementedInterfaces = type . GetInterfaces ( ) ;
7393 for ( var i = 0 ; i < implementedInterfaces . Length ; ++ i )
7494 {
7595 if ( typeComparer . Equals ( implementedInterfaces [ i ] , interfaceType ) )
@@ -88,7 +108,12 @@ public static bool Implements([ValidatedNotNull] this Type type, [ValidatedNotNu
88108 /// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="otherType" /> is null.</exception>
89109 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
90110 [ ContractAnnotation ( "type:null => halt; otherType:null => halt" ) ]
91- public static bool IsOrImplements ( [ ValidatedNotNull ] this Type type , [ ValidatedNotNull ] Type otherType ) =>
111+ public static bool IsOrImplements (
112+ #if NET8_0
113+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
114+ #endif
115+ [ ValidatedNotNull ] this Type type ,
116+ [ ValidatedNotNull ] Type otherType ) =>
92117 type . IsEquivalentTypeTo ( otherType . MustNotBeNull ( nameof ( otherType ) ) ) || type . Implements ( otherType ) ;
93118
94119 /// <summary>
@@ -102,7 +127,13 @@ public static bool IsOrImplements([ValidatedNotNull] this Type type, [ValidatedN
102127 /// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="otherType" /> is null.</exception>
103128 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
104129 [ ContractAnnotation ( "type:null => halt; otherType:null => halt" ) ]
105- public static bool IsOrImplements ( [ ValidatedNotNull ] this Type type , [ ValidatedNotNull ] Type otherType , [ ValidatedNotNull ] IEqualityComparer < Type > typeComparer ) =>
130+ public static bool IsOrImplements (
131+ #if NET8_0
132+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
133+ #endif
134+ [ ValidatedNotNull ] this Type type ,
135+ [ ValidatedNotNull ] Type otherType ,
136+ [ ValidatedNotNull ] IEqualityComparer < Type > typeComparer ) =>
106137 typeComparer . MustNotBeNull ( nameof ( typeComparer ) ) . Equals ( type . MustNotBeNull ( nameof ( type ) ) , otherType . MustNotBeNull ( nameof ( otherType ) ) ) || type . Implements ( otherType , typeComparer ) ;
107138
108139 /// <summary>
@@ -190,11 +221,16 @@ public static bool IsOrDerivesFrom([ValidatedNotNull] this Type type, [Validated
190221 /// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="baseClassOrInterfaceType" /> is null.</exception>
191222 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
192223 [ ContractAnnotation ( "type:null => halt; baseClassOrInterfaceType:null => halt" ) ]
193- public static bool InheritsFrom ( [ ValidatedNotNull ] this Type type , [ ValidatedNotNull ] Type baseClassOrInterfaceType ) =>
224+ public static bool InheritsFrom (
225+ #if NET8_0
226+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
227+ #endif
228+ [ ValidatedNotNull ] this Type type ,
229+ [ ValidatedNotNull ] Type baseClassOrInterfaceType ) =>
194230 baseClassOrInterfaceType . MustNotBeNull ( nameof ( baseClassOrInterfaceType ) )
195- . IsInterface
196- ? type . Implements ( baseClassOrInterfaceType )
197- : type . DerivesFrom ( baseClassOrInterfaceType ) ;
231+ . IsInterface ?
232+ type . Implements ( baseClassOrInterfaceType ) :
233+ type . DerivesFrom ( baseClassOrInterfaceType ) ;
198234
199235 /// <summary>
200236 /// Checks if the given type derives from the specified base class or interface type. This overload uses the specified <paramref name="typeComparer" />
@@ -206,11 +242,17 @@ public static bool InheritsFrom([ValidatedNotNull] this Type type, [ValidatedNot
206242 /// <exception cref="ArgumentNullException">Thrown when <paramref name="type" />, or <paramref name="baseClassOrInterfaceType" />, or <paramref name="typeComparer" /> is null.</exception>
207243 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
208244 [ ContractAnnotation ( "type:null => halt; baseClassOrInterfaceType:null => halt; typeComparer:null => halt" ) ]
209- public static bool InheritsFrom ( [ ValidatedNotNull ] this Type type , [ ValidatedNotNull ] Type baseClassOrInterfaceType , [ ValidatedNotNull ] IEqualityComparer < Type > typeComparer ) =>
245+ public static bool InheritsFrom (
246+ #if NET8_0
247+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
248+ #endif
249+ [ ValidatedNotNull ] this Type type ,
250+ [ ValidatedNotNull ] Type baseClassOrInterfaceType ,
251+ [ ValidatedNotNull ] IEqualityComparer < Type > typeComparer ) =>
210252 baseClassOrInterfaceType . MustNotBeNull ( nameof ( baseClassOrInterfaceType ) )
211- . IsInterface
212- ? type . Implements ( baseClassOrInterfaceType , typeComparer )
213- : type . DerivesFrom ( baseClassOrInterfaceType , typeComparer ) ;
253+ . IsInterface ?
254+ type . Implements ( baseClassOrInterfaceType , typeComparer ) :
255+ type . DerivesFrom ( baseClassOrInterfaceType , typeComparer ) ;
214256
215257 /// <summary>
216258 /// Checks if the given <paramref name="type" /> is equal to the specified <paramref name="otherType" /> or if it derives from it or implements it.
@@ -222,7 +264,12 @@ public static bool InheritsFrom([ValidatedNotNull] this Type type, [ValidatedNot
222264 /// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="otherType" /> is null.</exception>
223265 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
224266 [ ContractAnnotation ( "type:null => halt; otherType:null => halt" ) ]
225- public static bool IsOrInheritsFrom ( [ ValidatedNotNull ] this Type type , [ ValidatedNotNull ] Type otherType ) =>
267+ public static bool IsOrInheritsFrom (
268+ #if NET8_0
269+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
270+ #endif
271+ [ ValidatedNotNull ] this Type type ,
272+ [ ValidatedNotNull ] Type otherType ) =>
226273 type . IsEquivalentTypeTo ( otherType . MustNotBeNull ( nameof ( otherType ) ) ) || type . InheritsFrom ( otherType ) ;
227274
228275
@@ -236,7 +283,13 @@ public static bool IsOrInheritsFrom([ValidatedNotNull] this Type type, [Validate
236283 /// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="otherType" /> is null.</exception>
237284 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
238285 [ ContractAnnotation ( "type:null => halt; otherType:null => halt; typeComparer:null => halt" ) ]
239- public static bool IsOrInheritsFrom ( [ ValidatedNotNull ] this Type type , [ ValidatedNotNull ] Type otherType , [ ValidatedNotNull ] IEqualityComparer < Type > typeComparer ) =>
286+ public static bool IsOrInheritsFrom (
287+ #if NET8_0
288+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
289+ #endif
290+ [ ValidatedNotNull ] this Type type ,
291+ [ ValidatedNotNull ] Type otherType ,
292+ [ ValidatedNotNull ] IEqualityComparer < Type > typeComparer ) =>
240293 typeComparer . MustNotBeNull ( nameof ( typeComparer ) ) . Equals ( type , otherType . MustNotBeNull ( nameof ( otherType ) ) ) || type . InheritsFrom ( otherType , typeComparer ) ;
241294
242295
0 commit comments