🌐 AI搜索 & 代理 主页
Skip to content

Commit cf01b18

Browse files
generallokifilmor
authored andcommitted
MethodBinderEvents.CoerceBind
1 parent 9a3c04e commit cf01b18

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/runtime/MethodBinder.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
namespace Python.Runtime
1010
{
1111
using MaybeMethodBase = MaybeMethodBase<MethodBase>;
12+
13+
public delegate void MethodBinderCoerceBindDelegate(
14+
Dictionary<string, PyObject> arguments,
15+
MethodBase[] methods,
16+
ref MethodBase? foundBinding);
17+
1218
/// <summary>
1319
/// A MethodBinder encapsulates information about a (possibly overloaded)
1420
/// managed method, and is responsible for selecting the right method given
@@ -516,7 +522,7 @@ public MismatchedMethod(Exception exception, MethodBase mb)
516522
}
517523
}
518524

519-
return new Binding(mi, target, margs, outs);
525+
return CoerceResult(new Binding(mi, target, margs, outs));
520526
}
521527
else if (matchGenerics && isGeneric)
522528
{
@@ -528,7 +534,7 @@ public MismatchedMethod(Exception exception, MethodBase mb)
528534
MethodInfo[] overloads = MatchParameters(methods, types);
529535
if (overloads.Length != 0)
530536
{
531-
return Bind(inst, args, kwargDict, overloads, matchGenerics: false);
537+
return CoerceResult(Bind(inst, args, kwargDict, overloads, matchGenerics: false));
532538
}
533539
}
534540
if (mismatchedMethods.Count > 0)
@@ -537,6 +543,19 @@ public MismatchedMethod(Exception exception, MethodBase mb)
537543
Exceptions.SetError(aggregateException);
538544
}
539545
return null;
546+
547+
Binding? CoerceResult(Binding? binding)
548+
{
549+
if (binding is not null)
550+
{
551+
var foundMethod = binding.info;
552+
MethodBinderEvents.CoerceBind?.Invoke(kwargDict, methods, ref foundMethod);
553+
if (foundMethod is null)
554+
return null;
555+
}
556+
557+
return binding;
558+
}
540559
}
541560

542561
static AggregateException GetAggregateException(IEnumerable<MismatchedMethod> mismatchedMethods)
@@ -1068,4 +1087,9 @@ static internal class ParameterInfoExtensions
10681087
}
10691088
}
10701089
}
1090+
1091+
public static class MethodBinderEvents
1092+
{
1093+
public static MethodBinderCoerceBindDelegate? CoerceBind;
1094+
}
10711095
}

0 commit comments

Comments
 (0)