@@ -327,7 +327,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
327
327
_methods = GetMethods ( ) ;
328
328
}
329
329
330
- List < MatchedMethod > argMatchedMethods = new List < MatchedMethod > ( _methods . Length ) ;
330
+ var argMatchedMethods = new List < MatchedMethod > ( _methods . Length ) ;
331
331
332
332
// TODO: Clean up
333
333
foreach ( MethodBase mi in _methods )
@@ -359,33 +359,22 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
359
359
var matchedMethod = new MatchedMethod ( kwargsMatched , defaultsNeeded , margs , outs , mi ) ;
360
360
argMatchedMethods . Add ( matchedMethod ) ;
361
361
}
362
- if ( argMatchedMethods . Count ( ) > 0 )
362
+ if ( argMatchedMethods . Count > 0 )
363
363
{
364
- // Order matched methods by number of kwargs matched and get the max possible number
365
- // of kwargs matched
366
364
var bestKwargMatchCount = argMatchedMethods . Max ( x=> x . KwargsMatched ) ;
365
+ var fewestDefaultsRequired = argMatchedMethods . Where ( x=> x . KwargsMatched == bestKwargMatchCount ) . Min ( x=> x . DefaultsNeeded ) ;
367
366
368
- List < MatchedMethod > bestKwargMatches = new List < MatchedMethod > ( argMatchedMethods . Count ( ) ) ;
369
- foreach ( MatchedMethod testMatch in argMatchedMethods )
370
- {
371
- if ( testMatch . KwargsMatched == bestKwargMatchCount )
372
- {
373
- bestKwargMatches . Add ( testMatch ) ;
374
- }
375
- }
376
-
377
- // Order by the number of defaults required and find the smallest
378
- var fewestDefaultsRequired = bestKwargMatches . Min ( x=> x . DefaultsNeeded ) ;
379
367
int bestCount = 0 ;
380
368
int bestMatchIndex = - 1 ;
381
369
382
- foreach ( MatchedMethod testMatch in bestKwargMatches )
370
+ for ( int index = 0 ; index < argMatchedMethods . Count ; index ++ )
383
371
{
384
- if ( testMatch . DefaultsNeeded == fewestDefaultsRequired )
372
+ var testMatch = argMatchedMethods [ index ] ;
373
+ if ( testMatch . DefaultsNeeded == fewestDefaultsRequired && testMatch . KwargsMatched == bestKwargMatchCount )
385
374
{
386
375
bestCount ++ ;
387
376
if ( bestMatchIndex == - 1 )
388
- bestMatchIndex = bestKwargMatches . IndexOf ( testMatch ) ;
377
+ bestMatchIndex = index ;
389
378
}
390
379
}
391
380
@@ -403,7 +392,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
403
392
// in the case of (a) we're done by default. For (b) regardless of which
404
393
// method we choose, all arguments are specified _and_ can be converted
405
394
// from python to C# so picking any will suffice
406
- MatchedMethod bestMatch = bestKwargMatches . ElementAt ( bestMatchIndex ) ;
395
+ MatchedMethod bestMatch = argMatchedMethods [ bestMatchIndex ] ;
407
396
var margs = bestMatch . ManagedArgs ;
408
397
var outs = bestMatch . Outs ;
409
398
var mi = bestMatch . Method ;
@@ -619,7 +608,7 @@ static bool MatchesArgumentCount(int positionalArgumentCount, ParameterInfo[] pa
619
608
defaultArgList = null ;
620
609
var match = false ;
621
610
paramsArray = false ;
622
- var kwargCount = kwargDict . Count ( ) ;
611
+ var kwargCount = kwargDict . Count ;
623
612
kwargsMatched = 0 ;
624
613
defaultsNeeded = 0 ;
625
614