-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Description
Fix overload resolution to avoid AmbiguousMatchException and wrong method dispatch
_type.GetMethod(binder.Name) throws for overloaded methods and ignores argument shapes. Resolve by selecting the best candidate using argument count/types (and allowing a trailing CancellationToken).
Apply this diff to use a resolver:
- var method = _type.GetMethod(binder.Name);
- if (method == null)
+ var method = ResolveMethod(binder.Name, args);
+ if (method is null)
{
result = null;
return false;
}Add this helper inside the class:
private MethodInfo? ResolveMethod(string name, object?[]? args)
{
var candidates = _type.GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Where(m => m.Name == name)
.ToArray();
if (candidates.Length == 0) return null;
var argArray = args as object[] ?? Array.Empty<object>();
var argCount = argArray.Length;
foreach (var m in candidates)
{
var ps = m.GetParameters();
var hasCt = ps.Length > 0 && ps[^1].ParameterType == typeof(System.Threading.CancellationToken);
var matchCount = hasCt ? ps.Length - 1 : ps.Length;
if (matchCount != argCount) continue;
var ok = true;
for (var i = 0; i < matchCount; i++)
{
var a = argArray[i];
if (a is null) continue; // null is acceptable for ref/nullable params
if (!ps[i].ParameterType.IsInstanceOfType(a)) { ok = false; break; }
}
if (ok) return m;
}
// Fallback: first by name
return candidates.FirstOrDefault();
}And add the needed using:
using System.Reflection;
+using System.Threading;🤖 Prompt for AI Agents
In Activout.RestClient/Implementation/RestClient.cs around lines 31 to 36, the
current call to _type.GetMethod(binder.Name) fails for overloaded methods and
ignores argument shapes; add a ResolveMethod helper (as described in the review)
inside the class to pick the correct MethodInfo by filtering candidates by name,
parameter count/types and allowing a trailing CancellationToken, replace the
direct GetMethod call with ResolveMethod(binder.Name, args) (or similar) so you
select the best overload, and add the required using for BindingFlags/Reflection
at the top of the file.
Originally posted by @coderabbitai[bot] in #129 (comment)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels