@@ -363,7 +363,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
363
363
{
364
364
// Order matched methods by number of kwargs matched and get the max possible number
365
365
// of kwargs matched
366
- var bestKwargMatchCount = argMatchedMethods . OrderBy ( ( x ) => x . KwargsMatched ) . Reverse ( ) . ToArray ( ) [ 0 ] . KwargsMatched ;
366
+ var bestKwargMatchCount = argMatchedMethods . Max ( x => x . KwargsMatched ) ;
367
367
368
368
List < MatchedMethod > bestKwargMatches = new List < MatchedMethod > ( argMatchedMethods . Count ( ) ) ;
369
369
foreach ( MatchedMethod testMatch in argMatchedMethods )
@@ -375,18 +375,21 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
375
375
}
376
376
377
377
// 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 ;
379
381
380
- List < MatchedMethod > bestDefaultsMatches = new List < MatchedMethod > ( bestKwargMatches . Count ( ) ) ;
381
382
foreach ( MatchedMethod testMatch in bestKwargMatches )
382
383
{
383
384
if ( testMatch . DefaultsNeeded == fewestDefaultsRequired )
384
385
{
385
- bestDefaultsMatches . Add ( testMatch ) ;
386
+ bestCount ++ ;
387
+ if ( bestMatchIndex == - 1 )
388
+ bestMatchIndex = bestKwargMatches . IndexOf ( testMatch ) ;
386
389
}
387
390
}
388
391
389
- if ( bestDefaultsMatches . Count ( ) > 1 && fewestDefaultsRequired > 0 )
392
+ if ( bestCount > 1 && fewestDefaultsRequired > 0 )
390
393
{
391
394
// Best effort for determining method to match on gives multiple possible
392
395
// 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
400
403
// in the case of (a) we're done by default. For (b) regardless of which
401
404
// method we choose, all arguments are specified _and_ can be converted
402
405
// from python to C# so picking any will suffice
403
- MatchedMethod bestMatch = bestDefaultsMatches . ToArray ( ) [ 0 ] ;
406
+ MatchedMethod bestMatch = bestKwargMatches . ElementAt ( bestMatchIndex ) ;
404
407
var margs = bestMatch . ManagedArgs ;
405
408
var outs = bestMatch . Outs ;
406
409
var mi = bestMatch . Method ;