@@ -86,16 +86,17 @@ internal void AddMethod(MethodBase m)
8686
8787 /// <summary>
8888 /// Given a sequence of MethodInfo and a sequence of type parameters,
89- /// return the MethodInfo that represents the matching closed generic.
89+ /// return the MethodInfo(s) that represents the matching closed generic.
9090 /// If unsuccessful, returns null and may set a Python error.
9191 /// </summary>
92- internal static MethodInfo ? MatchParameters ( MethodBase [ ] mi , Type [ ] ? tp )
92+ internal static MethodInfo [ ] MatchParameters ( MethodBase [ ] mi , Type [ ] ? tp )
9393 {
9494 if ( tp == null )
9595 {
96- return null ;
96+ return Array . Empty < MethodInfo > ( ) ;
9797 }
9898 int count = tp . Length ;
99+ var result = new List < MethodInfo > ( ) ;
99100 foreach ( MethodInfo t in mi )
100101 {
101102 if ( ! t . IsGenericMethodDefinition )
@@ -111,16 +112,14 @@ internal void AddMethod(MethodBase m)
111112 {
112113 // MakeGenericMethod can throw ArgumentException if the type parameters do not obey the constraints.
113114 MethodInfo method = t . MakeGenericMethod ( tp ) ;
114- Exceptions . Clear ( ) ;
115- return method ;
115+ result . Add ( method ) ;
116116 }
117- catch ( ArgumentException e )
117+ catch ( ArgumentException )
118118 {
119- Exceptions . SetError ( e ) ;
120119 // The error will remain set until cleared by a successful match.
121120 }
122121 }
123- return null ;
122+ return result . ToArray ( ) ;
124123 }
125124
126125
@@ -381,9 +380,6 @@ public MismatchedMethod(Exception exception, MethodBase mb)
381380 }
382381 }
383382
384- var pynargs = ( int ) Runtime . PyTuple_Size ( args ) ;
385- var isGeneric = false ;
386-
387383 MethodBase [ ] _methods ;
388384 if ( info != null )
389385 {
@@ -395,11 +391,19 @@ public MismatchedMethod(Exception exception, MethodBase mb)
395391 _methods = GetMethods ( ) ;
396392 }
397393
398- var argMatchedMethods = new List < MatchedMethod > ( _methods . Length ) ;
394+ return Bind ( inst , args , kwargDict , _methods , matchGenerics : true ) ;
395+ }
396+
397+ static Binding ? Bind ( BorrowedReference inst , BorrowedReference args , Dictionary < string , PyObject > kwargDict , MethodBase [ ] methods , bool matchGenerics )
398+ {
399+ var pynargs = ( int ) Runtime . PyTuple_Size ( args ) ;
400+ var isGeneric = false ;
401+
402+ var argMatchedMethods = new List < MatchedMethod > ( methods . Length ) ;
399403 var mismatchedMethods = new List < MismatchedMethod > ( ) ;
400404
401405 // TODO: Clean up
402- foreach ( MethodBase mi in _methods )
406+ foreach ( MethodBase mi in methods )
403407 {
404408 if ( mi . IsGenericMethod )
405409 {
@@ -535,17 +539,17 @@ public MismatchedMethod(Exception exception, MethodBase mb)
535539
536540 return new Binding ( mi , target , margs , outs ) ;
537541 }
538- else if ( isGeneric && info == null && methodinfo != null )
542+ else if ( matchGenerics && isGeneric )
539543 {
540544 // We weren't able to find a matching method but at least one
541545 // is a generic method and info is null. That happens when a generic
542546 // method was not called using the [] syntax. Let's introspect the
543547 // type of the arguments and use it to construct the correct method.
544548 Type [ ] ? types = Runtime . PythonArgsToTypeArray ( args , true ) ;
545- MethodInfo ? mi = MatchParameters ( methodinfo , types ) ;
546- if ( mi != null )
549+ MethodInfo [ ] overloads = MatchParameters ( methods , types ) ;
550+ if ( overloads . Length != 0 )
547551 {
548- return Bind ( inst , args , kw , mi , null ) ;
552+ return Bind ( inst , args , kwargDict , overloads , matchGenerics : false ) ;
549553 }
550554 }
551555 if ( mismatchedMethods . Count > 0 )
0 commit comments