@@ -32,7 +32,7 @@ public int FindMethod(Type type, string methodName, bool staticAccess, ref Expre
32
32
foreach ( Type t in SelfAndBaseTypes ( type ) )
33
33
{
34
34
MemberInfo [ ] members = t . FindMembers ( MemberTypes . Method , flags , Type . FilterNameIgnoreCase , methodName ) ;
35
- int count = FindBestMethod ( members . Cast < MethodBase > ( ) , ref args , out method ) ;
35
+ int count = FindBestMethodBasedOnArguments ( members . Cast < MethodBase > ( ) , ref args , out method ) ;
36
36
if ( count != 0 )
37
37
{
38
38
return count ;
@@ -41,8 +41,8 @@ public int FindMethod(Type type, string methodName, bool staticAccess, ref Expre
41
41
#else
42
42
foreach ( Type t in SelfAndBaseTypes ( type ) )
43
43
{
44
- MethodInfo [ ] methods = t . GetTypeInfo ( ) . DeclaredMethods . Where ( x => ( x . IsStatic || ! staticAccess ) && x . Name . ToLowerInvariant ( ) == methodName . ToLowerInvariant ( ) ) . ToArray ( ) ;
45
- int count = FindBestMethod ( methods , ref args , out method ) ;
44
+ var methods = t . GetTypeInfo ( ) . DeclaredMethods . Where ( m => ( m . IsStatic || ! staticAccess ) && m . Name . Equals ( methodName , StringComparison . OrdinalIgnoreCase ) ) . ToArray ( ) ;
45
+ int count = FindBestMethodBasedOnArguments ( methods , ref args , out method ) ;
46
46
if ( count != 0 )
47
47
{
48
48
return count ;
@@ -52,13 +52,23 @@ public int FindMethod(Type type, string methodName, bool staticAccess, ref Expre
52
52
53
53
if ( instance != null )
54
54
{
55
- // Try to solve with registered extension methods
56
- if ( _parsingConfig . CustomTypeProvider . GetExtensionMethods ( ) . TryGetValue ( type , out var methods ) )
55
+ // Try to solve with registered extension methods from this type and all base types
56
+ var methods = new List < MethodInfo > ( ) ;
57
+ foreach ( var t in SelfAndBaseTypes ( type ) )
58
+ {
59
+ if ( _parsingConfig . CustomTypeProvider . GetExtensionMethods ( ) . TryGetValue ( t , out var extensionMethodsOfType ) )
60
+ {
61
+ methods . AddRange ( extensionMethodsOfType . Where ( m => m . Name . Equals ( methodName , StringComparison . OrdinalIgnoreCase ) && ! m . IsGenericMethod ) ) ;
62
+ }
63
+ }
64
+
65
+ if ( methods . Any ( ) )
57
66
{
58
67
var argsList = args . ToList ( ) ;
59
68
argsList . Insert ( 0 , instance ) ;
69
+
60
70
var extensionMethodArgs = argsList . ToArray ( ) ;
61
- int count = FindBestMethod ( methods . Cast < MethodBase > ( ) , ref extensionMethodArgs , out method ) ;
71
+ int count = FindBestMethodBasedOnArguments ( methods . Cast < MethodBase > ( ) , ref extensionMethodArgs , out method ) ;
62
72
if ( count != 0 )
63
73
{
64
74
instance = null ;
@@ -72,10 +82,9 @@ public int FindMethod(Type type, string methodName, bool staticAccess, ref Expre
72
82
return 0 ;
73
83
}
74
84
75
- public int FindBestMethod ( IEnumerable < MethodBase > methods , ref Expression [ ] args , out MethodBase method )
85
+ public int FindBestMethodBasedOnArguments ( IEnumerable < MethodBase > methods , ref Expression [ ] args , out MethodBase method )
76
86
{
77
- // passing args by reference is now required with the params array support.
78
-
87
+ // Passing args by reference is now required with the params array support.
79
88
var inlineArgs = args ;
80
89
81
90
MethodData [ ] applicable = methods
@@ -85,7 +94,7 @@ public int FindBestMethod(IEnumerable<MethodBase> methods, ref Expression[] args
85
94
86
95
if ( applicable . Length > 1 )
87
96
{
88
- applicable = applicable . Where ( m => applicable . All ( n => m == n || IsBetterThan ( inlineArgs , m , n ) ) ) . ToArray ( ) ;
97
+ applicable = applicable . Where ( m => applicable . All ( n => m == n || FirstIsBetterThanSecond ( inlineArgs , m , n ) ) ) . ToArray ( ) ;
89
98
}
90
99
91
100
if ( args . Length == 2 && applicable . Length > 1 && ( args [ 0 ] . Type == typeof ( Guid ? ) || args [ 1 ] . Type == typeof ( Guid ? ) ) )
@@ -121,7 +130,7 @@ public int FindIndexer(Type type, Expression[] args, out MethodBase method)
121
130
#else
122
131
Select ( p => ( MethodBase ) p . GetMethod ) ;
123
132
#endif
124
- int count = FindBestMethod ( methods , ref args , out method ) ;
133
+ int count = FindBestMethodBasedOnArguments ( methods , ref args , out method ) ;
125
134
if ( count != 0 )
126
135
{
127
136
return count ;
@@ -203,9 +212,9 @@ bool IsApplicable(MethodData method, Expression[] args)
203
212
return true ;
204
213
}
205
214
206
- bool IsBetterThan ( Expression [ ] args , MethodData first , MethodData second )
215
+ bool FirstIsBetterThanSecond ( Expression [ ] args , MethodData first , MethodData second )
207
216
{
208
- // If args count is 0 -> parametereless method is better than method method with parameters
217
+ // If args count is 0 -> parameterless method is better than method method with parameters
209
218
if ( args . Length == 0 )
210
219
{
211
220
return first . Parameters . Length == 0 && second . Parameters . Length != 0 ;
0 commit comments