@@ -327,7 +327,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
327327 _methods = GetMethods ( ) ;
328328 }
329329
330- List < MatchedMethod > argMatchedMethods = new List < MatchedMethod > ( _methods . Length ) ;
330+ var argMatchedMethods = new List < MatchedMethod > ( _methods . Length ) ;
331331
332332 // TODO: Clean up
333333 foreach ( MethodBase mi in _methods )
@@ -359,33 +359,22 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
359359 var matchedMethod = new MatchedMethod ( kwargsMatched , defaultsNeeded , margs , outs , mi ) ;
360360 argMatchedMethods . Add ( matchedMethod ) ;
361361 }
362- if ( argMatchedMethods . Count ( ) > 0 )
362+ if ( argMatchedMethods . Count > 0 )
363363 {
364- // Order matched methods by number of kwargs matched and get the max possible number
365- // of kwargs matched
366364 var bestKwargMatchCount = argMatchedMethods . Max ( x => x . KwargsMatched ) ;
365+ var fewestDefaultsRequired = argMatchedMethods . Where ( x => x . KwargsMatched == bestKwargMatchCount ) . Min ( x => x . DefaultsNeeded ) ;
367366
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 ) ;
379367 int bestCount = 0 ;
380368 int bestMatchIndex = - 1 ;
381369
382- foreach ( MatchedMethod testMatch in bestKwargMatches )
370+ for ( int index = 0 ; index < argMatchedMethods . Count ; index ++ )
383371 {
384- if ( testMatch . DefaultsNeeded == fewestDefaultsRequired )
372+ var testMatch = argMatchedMethods [ index ] ;
373+ if ( testMatch . DefaultsNeeded == fewestDefaultsRequired && testMatch . KwargsMatched == bestKwargMatchCount )
385374 {
386375 bestCount ++ ;
387376 if ( bestMatchIndex == - 1 )
388- bestMatchIndex = bestKwargMatches . IndexOf ( testMatch ) ;
377+ bestMatchIndex = index ;
389378 }
390379 }
391380
@@ -403,7 +392,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
403392 // in the case of (a) we're done by default. For (b) regardless of which
404393 // method we choose, all arguments are specified _and_ can be converted
405394 // from python to C# so picking any will suffice
406- MatchedMethod bestMatch = bestKwargMatches . ElementAt ( bestMatchIndex ) ;
395+ MatchedMethod bestMatch = argMatchedMethods [ bestMatchIndex ] ;
407396 var margs = bestMatch . ManagedArgs ;
408397 var outs = bestMatch . Outs ;
409398 var mi = bestMatch . Method ;
@@ -619,7 +608,7 @@ static bool MatchesArgumentCount(int positionalArgumentCount, ParameterInfo[] pa
619608 defaultArgList = null ;
620609 var match = false ;
621610 paramsArray = false ;
622- var kwargCount = kwargDict . Count ( ) ;
611+ var kwargCount = kwargDict . Count ;
623612 kwargsMatched = 0 ;
624613 defaultsNeeded = 0 ;
625614
0 commit comments