2
2
using System . Collections . Generic ;
3
3
using System . Runtime . CompilerServices ;
4
4
using JetBrains . Annotations ;
5
+ #if NET8_0
6
+ using System . Diagnostics . CodeAnalysis ;
7
+ #endif
5
8
6
9
namespace Light . GuardClauses ;
7
10
@@ -41,11 +44,19 @@ private static bool CheckTypeEquivalency(Type type, Type other)
41
44
/// <param name="interfaceType">The interface type that <paramref name="type" /> should implement.</param>
42
45
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="interfaceType" /> is null.</exception>
43
46
[ 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
+ )
45
55
{
46
- interfaceType . MustNotBeNull ( nameof ( interfaceType ) ) ;
47
- var implementedInterfaces = type . MustNotBeNull ( nameof ( type ) ) . GetInterfaces ( ) ;
56
+ type . MustNotBeNull ( ) ;
57
+ interfaceType . MustNotBeNull ( ) ;
48
58
59
+ var implementedInterfaces = type . GetInterfaces ( ) ;
49
60
for ( var i = 0 ; i < implementedInterfaces . Length ; ++ i )
50
61
{
51
62
if ( interfaceType . IsEquivalentTypeTo ( implementedInterfaces [ i ] ) )
@@ -64,12 +75,21 @@ public static bool Implements([ValidatedNotNull] this Type type, [ValidatedNotNu
64
75
/// <param name="typeComparer">The equality comparer used to compare the interface types.</param>
65
76
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type" />, or <paramref name="interfaceType" />, or <paramref name="typeComparer" /> is null.</exception>
66
77
[ 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
+ )
68
87
{
69
- interfaceType . MustNotBeNull ( nameof ( interfaceType ) ) ;
70
- typeComparer . MustNotBeNull ( nameof ( typeComparer ) ) ;
88
+ type . MustNotBeNull ( ) ;
89
+ interfaceType . MustNotBeNull ( ) ;
90
+ typeComparer . MustNotBeNull ( ) ;
71
91
72
- var implementedInterfaces = type . MustNotBeNull ( nameof ( type ) ) . GetInterfaces ( ) ;
92
+ var implementedInterfaces = type . GetInterfaces ( ) ;
73
93
for ( var i = 0 ; i < implementedInterfaces . Length ; ++ i )
74
94
{
75
95
if ( typeComparer . Equals ( implementedInterfaces [ i ] , interfaceType ) )
@@ -88,7 +108,12 @@ public static bool Implements([ValidatedNotNull] this Type type, [ValidatedNotNu
88
108
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="otherType" /> is null.</exception>
89
109
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
90
110
[ 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 ) =>
92
117
type . IsEquivalentTypeTo ( otherType . MustNotBeNull ( nameof ( otherType ) ) ) || type . Implements ( otherType ) ;
93
118
94
119
/// <summary>
@@ -102,7 +127,13 @@ public static bool IsOrImplements([ValidatedNotNull] this Type type, [ValidatedN
102
127
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="otherType" /> is null.</exception>
103
128
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
104
129
[ 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 ) =>
106
137
typeComparer . MustNotBeNull ( nameof ( typeComparer ) ) . Equals ( type . MustNotBeNull ( nameof ( type ) ) , otherType . MustNotBeNull ( nameof ( otherType ) ) ) || type . Implements ( otherType , typeComparer ) ;
107
138
108
139
/// <summary>
@@ -190,11 +221,16 @@ public static bool IsOrDerivesFrom([ValidatedNotNull] this Type type, [Validated
190
221
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="baseClassOrInterfaceType" /> is null.</exception>
191
222
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
192
223
[ 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 ) =>
194
230
baseClassOrInterfaceType . MustNotBeNull ( nameof ( baseClassOrInterfaceType ) )
195
- . IsInterface
196
- ? type . Implements ( baseClassOrInterfaceType )
197
- : type . DerivesFrom ( baseClassOrInterfaceType ) ;
231
+ . IsInterface ?
232
+ type . Implements ( baseClassOrInterfaceType ) :
233
+ type . DerivesFrom ( baseClassOrInterfaceType ) ;
198
234
199
235
/// <summary>
200
236
/// 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
206
242
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type" />, or <paramref name="baseClassOrInterfaceType" />, or <paramref name="typeComparer" /> is null.</exception>
207
243
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
208
244
[ 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 ) =>
210
252
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 ) ;
214
256
215
257
/// <summary>
216
258
/// 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
222
264
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="otherType" /> is null.</exception>
223
265
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
224
266
[ 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 ) =>
226
273
type . IsEquivalentTypeTo ( otherType . MustNotBeNull ( nameof ( otherType ) ) ) || type . InheritsFrom ( otherType ) ;
227
274
228
275
@@ -236,7 +283,13 @@ public static bool IsOrInheritsFrom([ValidatedNotNull] this Type type, [Validate
236
283
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="otherType" /> is null.</exception>
237
284
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
238
285
[ 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 ) =>
240
293
typeComparer . MustNotBeNull ( nameof ( typeComparer ) ) . Equals ( type , otherType . MustNotBeNull ( nameof ( otherType ) ) ) || type . InheritsFrom ( otherType , typeComparer ) ;
241
294
242
295
0 commit comments