@@ -363,7 +363,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
363363 {
364364 // Order matched methods by number of kwargs matched and get the max possible number
365365 // of kwargs matched
366- var bestKwargMatchCount = argMatchedMethods . OrderBy ( ( x ) => x . KwargsMatched ) . Reverse ( ) . ToArray ( ) [ 0 ] . KwargsMatched ;
366+ var bestKwargMatchCount = argMatchedMethods . Max ( x => x . KwargsMatched ) ;
367367
368368 List < MatchedMethod > bestKwargMatches = new List < MatchedMethod > ( argMatchedMethods . Count ( ) ) ;
369369 foreach ( MatchedMethod testMatch in argMatchedMethods )
@@ -375,18 +375,21 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
375375 }
376376
377377 // Order by the number of defaults required and find the smallest
378- var fewestDefaultsRequired = bestKwargMatches . OrderBy ( ( x ) => x . DefaultsNeeded ) . ToArray ( ) [ 0 ] . DefaultsNeeded ;
378+ var fewestDefaultsRequired = bestKwargMatches . Min ( x => x . DefaultsNeeded ) ;
379+ int bestCount = 0 ;
380+ int bestMatchIndex = - 1 ;
379381
380- List < MatchedMethod > bestDefaultsMatches = new List < MatchedMethod > ( bestKwargMatches . Count ( ) ) ;
381382 foreach ( MatchedMethod testMatch in bestKwargMatches )
382383 {
383384 if ( testMatch . DefaultsNeeded == fewestDefaultsRequired )
384385 {
385- bestDefaultsMatches . Add ( testMatch ) ;
386+ bestCount ++ ;
387+ if ( bestMatchIndex == - 1 )
388+ bestMatchIndex = bestKwargMatches . IndexOf ( testMatch ) ;
386389 }
387390 }
388391
389- if ( bestDefaultsMatches . Count ( ) > 1 && fewestDefaultsRequired > 0 )
392+ if ( bestCount > 1 && fewestDefaultsRequired > 0 )
390393 {
391394 // Best effort for determining method to match on gives multiple possible
392395 // matches and we need at least one default argument - bail from this point
@@ -400,7 +403,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
400403 // in the case of (a) we're done by default. For (b) regardless of which
401404 // method we choose, all arguments are specified _and_ can be converted
402405 // from python to C# so picking any will suffice
403- MatchedMethod bestMatch = bestDefaultsMatches . ToArray ( ) [ 0 ] ;
406+ MatchedMethod bestMatch = bestKwargMatches . ElementAt ( bestMatchIndex ) ;
404407 var margs = bestMatch . ManagedArgs ;
405408 var outs = bestMatch . Outs ;
406409 var mi = bestMatch . Method ;
0 commit comments