Skip to content

Commit 3b56057

Browse files
committed
Improve method matching logic in InlineQueryHandler.cs
Introduce a list to store potential matching methods and correct the condition to skip non-matching methods. Remove previous parameter conversion logic and add all matching methods to the list. After the loop, handle cases where exactly one or multiple methods match by converting parameters or throwing an AmbiguousMatchException, respectively. This ensures precise method selection or appropriate error handling.
1 parent a087ab0 commit 3b56057

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

Sources/TelegramBot/Handlers/InlineQueryHandler.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public InlineQueryHandler(IReadOnlyCollection<MethodInfo> controllerMethods, Upd
2626
}
2727
var inlineQuery = update.CallbackQuery;
2828
string command = inlineQuery.Data!;
29+
List<MethodInfo> methods = new List<MethodInfo>();
2930
foreach (var method in controllerMethods)
3031
{
3132
var attributes = method.GetCustomAttributes(typeof(InlineCommandAttribute), false);
@@ -53,20 +54,26 @@ public InlineQueryHandler(IReadOnlyCollection<MethodInfo> controllerMethods, Upd
5354
_args.Add(incomingCommandParts[i]);
5455
}
5556
}
56-
if (match)
57+
if (!match)
5758
{
58-
var args = _args.ToArray();
59-
bool converted = ObjectHelpers.TryConvertParameters(method, args);
60-
if (converted)
61-
{
62-
_args.Clear();
63-
_args.AddRange(args);
64-
return method;
65-
}
59+
continue;
6660
}
61+
methods.Add(method);
6762
}
6863
}
6964
}
65+
if (methods.Count == 1)
66+
{
67+
var args = _args.ToArray();
68+
ObjectHelpers.TryConvertParameters(methods[0], args);
69+
_args.Clear();
70+
_args.AddRange(args);
71+
return methods[0];
72+
}
73+
else if (methods.Count > 1)
74+
{
75+
throw new AmbiguousMatchException("Multiple methods found with the same command and arguments: " + command);
76+
}
7077
return null;
7178
}
7279

0 commit comments

Comments
 (0)