@@ -6,6 +6,24 @@ namespace System.Linq.Dynamic.Core.Parser;
66
77internal static class TypeHelper
88{
9+ internal static bool TryGetAsEnumerable ( Type type , [ NotNullWhen ( true ) ] out Type ? enumerableType )
10+ {
11+ if ( type . IsArray )
12+ {
13+ enumerableType = typeof ( IEnumerable < > ) . MakeGenericType ( type . GetElementType ( ) ! ) ;
14+ return true ;
15+ }
16+
17+ if ( type . GetTypeInfo ( ) . IsGenericType && type . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > ) )
18+ {
19+ enumerableType = type ;
20+ return true ;
21+ }
22+
23+ enumerableType = null ;
24+ return false ;
25+ }
26+
927 public static bool TryGetFirstGenericArgument ( Type type , [ NotNullWhen ( true ) ] out Type ? genericType )
1028 {
1129 var genericArguments = type . GetTypeInfo ( ) . GetGenericTypeArguments ( ) ;
@@ -196,79 +214,79 @@ public static bool IsCompatibleWith(Type source, Type target)
196214 }
197215 return false ;
198216#else
199- if ( source == target )
200- {
201- return true ;
202- }
217+ if ( source == target )
218+ {
219+ return true ;
220+ }
203221
204- if ( ! target . GetTypeInfo ( ) . IsValueType )
205- {
206- return target . IsAssignableFrom ( source ) ;
207- }
222+ if ( ! target . GetTypeInfo ( ) . IsValueType )
223+ {
224+ return target . IsAssignableFrom ( source ) ;
225+ }
208226
209- Type st = GetNonNullableType ( source ) ;
210- Type tt = GetNonNullableType ( target ) ;
227+ Type st = GetNonNullableType ( source ) ;
228+ Type tt = GetNonNullableType ( target ) ;
211229
212- if ( st != source && tt == target )
213- {
214- return false ;
215- }
230+ if ( st != source && tt == target )
231+ {
232+ return false ;
233+ }
216234
217- Type sc = st . GetTypeInfo ( ) . IsEnum ? typeof ( object ) : st ;
218- Type tc = tt . GetTypeInfo ( ) . IsEnum ? typeof ( object ) : tt ;
235+ Type sc = st . GetTypeInfo ( ) . IsEnum ? typeof ( object ) : st ;
236+ Type tc = tt . GetTypeInfo ( ) . IsEnum ? typeof ( object ) : tt ;
219237
220- if ( sc == typeof ( sbyte ) )
221- {
222- if ( tc == typeof ( sbyte ) || tc == typeof ( short ) || tc == typeof ( int ) || tc == typeof ( long ) || tc == typeof ( float ) || tc == typeof ( double ) || tc == typeof ( decimal ) )
223- return true ;
224- }
225- else if ( sc == typeof ( byte ) )
226- {
227- if ( tc == typeof ( byte ) || tc == typeof ( short ) || tc == typeof ( ushort ) || tc == typeof ( int ) || tc == typeof ( uint ) || tc == typeof ( long ) || tc == typeof ( ulong ) || tc == typeof ( float ) || tc == typeof ( double ) || tc == typeof ( decimal ) )
228- return true ;
229- }
230- else if ( sc == typeof ( short ) )
231- {
232- if ( tc == typeof ( short ) || tc == typeof ( int ) || tc == typeof ( long ) || tc == typeof ( float ) || tc == typeof ( double ) || tc == typeof ( decimal ) )
233- return true ;
234- }
235- else if ( sc == typeof ( ushort ) )
236- {
237- if ( tc == typeof ( ushort ) || tc == typeof ( int ) || tc == typeof ( uint ) || tc == typeof ( long ) || tc == typeof ( ulong ) || tc == typeof ( float ) || tc == typeof ( double ) || tc == typeof ( decimal ) )
238- return true ;
239- }
240- else if ( sc == typeof ( int ) )
241- {
242- if ( tc == typeof ( int ) || tc == typeof ( long ) || tc == typeof ( float ) || tc == typeof ( double ) || tc == typeof ( decimal ) )
243- return true ;
244- }
245- else if ( sc == typeof ( uint ) )
246- {
247- if ( tc == typeof ( uint ) || tc == typeof ( long ) || tc == typeof ( ulong ) || tc == typeof ( float ) || tc == typeof ( double ) || tc == typeof ( decimal ) )
248- return true ;
249- }
250- else if ( sc == typeof ( long ) )
251- {
252- if ( tc == typeof ( long ) || tc == typeof ( float ) || tc == typeof ( double ) || tc == typeof ( decimal ) )
253- return true ;
254- }
255- else if ( sc == typeof ( ulong ) )
256- {
257- if ( tc == typeof ( ulong ) || tc == typeof ( float ) || tc == typeof ( double ) || tc == typeof ( decimal ) )
258- return true ;
259- }
260- else if ( sc == typeof ( float ) )
261- {
262- if ( tc == typeof ( float ) || tc == typeof ( double ) )
263- return true ;
264- }
265-
266- if ( st == tt )
267- {
238+ if ( sc == typeof ( sbyte ) )
239+ {
240+ if ( tc == typeof ( sbyte ) || tc == typeof ( short ) || tc == typeof ( int ) || tc == typeof ( long ) || tc == typeof ( float ) || tc == typeof ( double ) || tc == typeof ( decimal ) )
268241 return true ;
269- }
242+ }
243+ else if ( sc == typeof ( byte ) )
244+ {
245+ if ( tc == typeof ( byte ) || tc == typeof ( short ) || tc == typeof ( ushort ) || tc == typeof ( int ) || tc == typeof ( uint ) || tc == typeof ( long ) || tc == typeof ( ulong ) || tc == typeof ( float ) || tc == typeof ( double ) || tc == typeof ( decimal ) )
246+ return true ;
247+ }
248+ else if ( sc == typeof ( short ) )
249+ {
250+ if ( tc == typeof ( short ) || tc == typeof ( int ) || tc == typeof ( long ) || tc == typeof ( float ) || tc == typeof ( double ) || tc == typeof ( decimal ) )
251+ return true ;
252+ }
253+ else if ( sc == typeof ( ushort ) )
254+ {
255+ if ( tc == typeof ( ushort ) || tc == typeof ( int ) || tc == typeof ( uint ) || tc == typeof ( long ) || tc == typeof ( ulong ) || tc == typeof ( float ) || tc == typeof ( double ) || tc == typeof ( decimal ) )
256+ return true ;
257+ }
258+ else if ( sc == typeof ( int ) )
259+ {
260+ if ( tc == typeof ( int ) || tc == typeof ( long ) || tc == typeof ( float ) || tc == typeof ( double ) || tc == typeof ( decimal ) )
261+ return true ;
262+ }
263+ else if ( sc == typeof ( uint ) )
264+ {
265+ if ( tc == typeof ( uint ) || tc == typeof ( long ) || tc == typeof ( ulong ) || tc == typeof ( float ) || tc == typeof ( double ) || tc == typeof ( decimal ) )
266+ return true ;
267+ }
268+ else if ( sc == typeof ( long ) )
269+ {
270+ if ( tc == typeof ( long ) || tc == typeof ( float ) || tc == typeof ( double ) || tc == typeof ( decimal ) )
271+ return true ;
272+ }
273+ else if ( sc == typeof ( ulong ) )
274+ {
275+ if ( tc == typeof ( ulong ) || tc == typeof ( float ) || tc == typeof ( double ) || tc == typeof ( decimal ) )
276+ return true ;
277+ }
278+ else if ( sc == typeof ( float ) )
279+ {
280+ if ( tc == typeof ( float ) || tc == typeof ( double ) )
281+ return true ;
282+ }
270283
271- return false ;
284+ if ( st == tt )
285+ {
286+ return true ;
287+ }
288+
289+ return false ;
272290#endif
273291 }
274292
@@ -391,19 +409,19 @@ private static int GetNumericTypeKind(Type type)
391409 return 0 ;
392410 }
393411#else
394- if ( type. GetTypeInfo( ) . IsEnum)
395- {
396- return 0 ;
397- }
412+ if ( type. GetTypeInfo( ) . IsEnum)
413+ {
414+ return 0 ;
415+ }
398416
399- if ( type == typeof ( char ) || type == typeof ( float ) || type == typeof ( double ) || type == typeof ( decimal ) )
400- return 1 ;
401- if ( type == typeof ( sbyte ) || type == typeof ( short ) || type == typeof ( int ) || type == typeof ( long ) )
402- return 2 ;
403- if ( type == typeof ( byte ) || type == typeof ( ushort ) || type == typeof ( uint ) || type == typeof ( ulong ) )
404- return 3 ;
417+ if ( type == typeof ( char ) || type == typeof ( float ) || type == typeof ( double ) || type == typeof ( decimal ) )
418+ return 1 ;
419+ if ( type == typeof ( sbyte ) || type == typeof ( short ) || type == typeof ( int ) || type == typeof ( long ) )
420+ return 2 ;
421+ if ( type == typeof ( byte ) || type == typeof ( ushort ) || type == typeof ( uint ) || type == typeof ( ulong ) )
422+ return 3 ;
405423
406- return 0 ;
424+ return 0 ;
407425#endif
408426 }
409427
@@ -484,7 +502,7 @@ private static void AddInterface(ICollection<Type> types, Type type)
484502
485503 public static bool TryParseEnum( string value, Type? type, [ NotNullWhen( true) ] out object ? enumValue)
486504 {
487- if ( type is { } && type. GetTypeInfo( ) . IsEnum && Enum. IsDefined( type, value) )
505+ if ( type != null && type. GetTypeInfo( ) . IsEnum && Enum. IsDefined( type, value) )
488506 {
489507 enumValue = Enum. Parse( type, value, true) ;
490508 return true;
0 commit comments