/// <summary>
/// Given a sequence of MethodInfo and a sequence of type parameters,
/// return the MethodInfo that represents the matching closed generic.
/// If unsuccessful, returns null and may set a Python error.
/// </summary>
internal static MethodInfo MatchParameters(MethodInfo[] mi, Type[] tp)
{
if (tp == null)
{
return(null);
}
int count = tp.Length;
foreach (MethodInfo t in mi)
{
if (!t.IsGenericMethodDefinition)
{
continue;
}
Type[] args = t.GetGenericArguments();
if (args.Length != count)
{
continue;
}
try
{
// MakeGenericMethod can throw ArgumentException if the type parameters do not obey the constraints.
MethodInfo method = t.MakeGenericMethod(tp);
Exceptions.Clear();
return(method);
}
catch (ArgumentException e)
{
Exceptions.SetError(e);
// The error will remain set until cleared by a successful match.
}
}
return(null);
}