@@ -6,55 +6,53 @@ namespace System.Linq.Dynamic.Core.Parser;
66
77internal class PredefinedMethodsHelper
88{
9- private static readonly BindingFlags _publicInstance = BindingFlags . Public | BindingFlags . Instance ;
10- private static readonly BindingFlags _publicStatic = BindingFlags . Public | BindingFlags . Static ;
9+ private const BindingFlags PublicInstance = BindingFlags . Public | BindingFlags . Instance ;
10+ private const BindingFlags PublicStatic = BindingFlags . Public | BindingFlags . Static ;
1111
12- internal static readonly MethodInfo ObjectInstanceToString = typeof ( object ) . GetMethod ( nameof ( ToString ) , _publicInstance , null , Type . EmptyTypes , null ) ! ;
13- internal static readonly MethodInfo ObjectInstanceEquals = typeof ( object ) . GetMethod ( nameof ( Equals ) , _publicInstance , null , [ typeof ( object ) ] , null ) ! ;
14- internal static readonly MethodInfo ObjectStaticEquals = typeof ( object ) . GetMethod ( nameof ( Equals ) , _publicStatic , null , [ typeof ( object ) , typeof ( object ) ] , null ) ! ;
15- internal static readonly MethodInfo ObjectStaticReferenceEquals = typeof ( object ) . GetMethod ( nameof ( ReferenceEquals ) , _publicStatic , null , [ typeof ( object ) , typeof ( object ) ] , null ) ! ;
12+ internal static readonly MethodInfo ObjectInstanceToString = typeof ( object ) . GetMethod ( nameof ( ToString ) , PublicInstance , null , Type . EmptyTypes , null ) ! ;
13+ internal static readonly MethodInfo ObjectInstanceEquals = typeof ( object ) . GetMethod ( nameof ( Equals ) , PublicInstance , null , [ typeof ( object ) ] , null ) ! ;
14+ internal static readonly MethodInfo ObjectStaticEquals = typeof ( object ) . GetMethod ( nameof ( Equals ) , PublicStatic , null , [ typeof ( object ) , typeof ( object ) ] , null ) ! ;
15+ internal static readonly MethodInfo ObjectStaticReferenceEquals = typeof ( object ) . GetMethod ( nameof ( ReferenceEquals ) , PublicStatic , null , [ typeof ( object ) , typeof ( object ) ] , null ) ! ;
1616
1717 private readonly Dictionary < Type , HashSet < MemberInfo > > _supported = new ( )
1818 {
19- { typeof ( bool ) , new HashSet < MemberInfo > ( ) } ,
20- { typeof ( char ) , new HashSet < MemberInfo > ( ) } ,
21- { typeof ( string ) , new HashSet < MemberInfo > ( ) } ,
22- { typeof ( sbyte ) , new HashSet < MemberInfo > ( ) } ,
23- { typeof ( byte ) , new HashSet < MemberInfo > ( ) } ,
24- { typeof ( short ) , new HashSet < MemberInfo > ( ) } ,
25- { typeof ( ushort ) , new HashSet < MemberInfo > ( ) } ,
26- { typeof ( int ) , new HashSet < MemberInfo > ( ) } ,
27- { typeof ( uint ) , new HashSet < MemberInfo > ( ) } ,
28- { typeof ( long ) , new HashSet < MemberInfo > ( ) } ,
29- { typeof ( ulong ) , new HashSet < MemberInfo > ( ) } ,
30- { typeof ( float ) , new HashSet < MemberInfo > ( ) } ,
31- { typeof ( double ) , new HashSet < MemberInfo > ( ) } ,
32- { typeof ( decimal ) , new HashSet < MemberInfo > ( ) } ,
33- { typeof ( DateTime ) , new HashSet < MemberInfo > ( ) } ,
34- { typeof ( DateTimeOffset ) , new HashSet < MemberInfo > ( ) } ,
35- { typeof ( TimeSpan ) , new HashSet < MemberInfo > ( ) } ,
36- { typeof ( Guid ) , new HashSet < MemberInfo > ( ) } ,
37- { typeof ( Uri ) , new HashSet < MemberInfo > ( ) } ,
38- { typeof ( Enum ) , new HashSet < MemberInfo > ( ) } ,
19+ { typeof ( bool ) , [ ] } ,
20+ { typeof ( byte ) , [ ] } ,
21+ { typeof ( char ) , [ ] } ,
22+ { typeof ( DateTime ) , [ ] } ,
23+ { typeof ( DateTimeOffset ) , [ ] } ,
24+ { typeof ( decimal ) , [ ] } ,
25+ { typeof ( double ) , [ ] } ,
26+ // { typeof(Enum ), [] },
27+ { typeof ( float ) , [ ] } ,
28+ { typeof ( Guid ) , [ ] } ,
29+ { typeof ( int ) , [ ] } ,
30+ { typeof ( long ) , [ ] } ,
31+ { typeof ( sbyte ) , [ ] } ,
32+ { typeof ( short ) , [ ] } ,
33+ { typeof ( string ) , [ ] } ,
34+ { typeof ( TimeSpan ) , [ ] } ,
35+ { typeof ( uint ) , [ ] } ,
36+ { typeof ( ulong ) , [ ] } ,
37+ { typeof ( Uri ) , [ ] } ,
38+ { typeof ( ushort ) , [ ] } ,
3939#if NET6_0_OR_GREATER
40- { typeof ( DateOnly ) , new HashSet < MemberInfo > ( ) } ,
41- { typeof ( TimeOnly ) , new HashSet < MemberInfo > ( ) } ,
40+ { typeof ( DateOnly ) , [ ] } ,
41+ { typeof ( TimeOnly ) , [ ] } ,
4242#endif
4343 } ;
4444
4545 public PredefinedMethodsHelper ( ParsingConfig config )
4646 {
4747 foreach ( var kvp in _supported )
4848 {
49- TryAdd ( kvp . Key , ObjectInstanceEquals ) ;
50- TryAdd ( kvp . Key , kvp . Key . GetMethod ( nameof ( Equals ) , _publicInstance , null , [ kvp . Key ] , null ) ) ;
51- TryAdd ( kvp . Key , kvp . Key . GetMethod ( nameof ( Equals ) , _publicInstance , null , [ typeof ( object ) ] , null ) ) ;
49+ TryAdd ( kvp . Key , kvp . Key . GetMethod ( nameof ( Equals ) , PublicInstance , null , [ kvp . Key ] , null ) ) ;
50+ TryAdd ( kvp . Key , kvp . Key . GetMethod ( nameof ( Equals ) , PublicInstance , null , [ typeof ( object ) ] , null ) ) ;
5251
53- TryAdd ( kvp . Key , ObjectInstanceToString ) ;
54- TryAdd ( kvp . Key , kvp . Key . GetMethod ( nameof ( ToString ) , _publicInstance , null , Type . EmptyTypes , null ) ) ;
55- TryAdd ( kvp . Key , kvp . Key . GetMethod ( nameof ( ToString ) , _publicInstance , null , [ typeof ( string ) ] , null ) ) ;
56- TryAdd ( kvp . Key , kvp . Key . GetMethod ( nameof ( ToString ) , _publicInstance , null , [ typeof ( IFormatProvider ) ] , null ) ) ;
57- TryAdd ( kvp . Key , kvp . Key . GetMethod ( nameof ( ToString ) , _publicInstance , null , [ typeof ( string ) , typeof ( IFormatProvider ) ] , null ) ) ;
52+ TryAdd ( kvp . Key , kvp . Key . GetMethod ( nameof ( ToString ) , PublicInstance , null , Type . EmptyTypes , null ) ) ;
53+ TryAdd ( kvp . Key , kvp . Key . GetMethod ( nameof ( ToString ) , PublicInstance , null , [ typeof ( string ) ] , null ) ) ;
54+ TryAdd ( kvp . Key , kvp . Key . GetMethod ( nameof ( ToString ) , PublicInstance , null , [ typeof ( IFormatProvider ) ] , null ) ) ;
55+ TryAdd ( kvp . Key , kvp . Key . GetMethod ( nameof ( ToString ) , PublicInstance , null , [ typeof ( string ) , typeof ( IFormatProvider ) ] , null ) ) ;
5856 }
5957
6058 if ( config . AllowEqualsAndToStringMethodsOnObject )
@@ -63,17 +61,28 @@ public PredefinedMethodsHelper(ParsingConfig config)
6361 }
6462 }
6563
66- public bool IsPredefinedMethod ( Type type , MemberInfo member )
64+ public bool IsPredefinedMethod ( Type type , Type declaringType , MemberInfo member )
6765 {
6866 Check . NotNull ( type ) ;
6967 Check . NotNull ( member ) ;
7068
71- if ( ! _supported . TryGetValue ( type , out var supported ) || supported . Count == 0 )
69+ if ( _supported . TryGetValue ( type , out var supportedMethodsForType ) && supportedMethodsForType . Count > 0 )
7270 {
73- return false ;
71+ return supportedMethodsForType . Contains ( member ) ;
7472 }
7573
76- return supported . Contains ( member ) ;
74+ if ( _supported . TryGetValue ( declaringType , out var supportedMethodsForDeclaringType ) && supportedMethodsForDeclaringType . Count > 0 )
75+ {
76+ return supportedMethodsForDeclaringType . Contains ( member ) ;
77+ }
78+
79+ // Last resort, check if the method name is supported for object
80+ if ( _supported . TryGetValue ( typeof ( object ) , out var supportedMethodsForObject ) && supportedMethodsForObject . Count > 0 )
81+ {
82+ return supportedMethodsForObject . Any ( x => x . Name == member . Name ) ;
83+ }
84+
85+ return false ;
7786 }
7887
7988 private void TryAdd ( Type type , MethodInfo ? method )
0 commit comments