public static IEnumerable<MethodBase> FindBestMatch(Engine engine, MethodBase[] methods, JsValue[] arguments) { methods = methods .Where(m => m.GetParameters().Count() == arguments.Length || (m.IsGenericMethodDefinition && m.GetGenericArguments().Count() == arguments.Length)) .ToArray(); if (methods.Length == 1 && !methods[0].GetParameters().Any()) { yield return methods[0]; yield break; } var objectArguments = arguments.Select(x => x.ToObject()).ToArray(); foreach (var method in methods) { var perfectMatch = true; var parameters = method.GetParameters(); for (var i = 0; i < arguments.Length; i++) { var arg = objectArguments[i]; var paramType = parameters[i].ParameterType; if (arg == null) { if (!TypeIsNullable(paramType)) { perfectMatch = false; break; } } else if (arg.GetType() != paramType) { perfectMatch = false; break; } } if (perfectMatch) { yield return method; yield break; } } foreach (var method in methods) { yield return method; } }
public static IEnumerable<MethodBase> FindBestMatch(Engine engine, MethodBase[] methods, JsValue[] arguments) { methods = methods .Where(m => m.GetParameters().Count() == arguments.Length) .ToArray() ; if (methods.Length == 1 && !methods[0].GetParameters().Any()) { yield return methods[0]; yield break; } var objectArguments = arguments.Select(x => x.ToObject()).ToArray(); foreach (var method in methods) { var perfectMatch = true; var parameters = method.GetParameters(); for (var i = 0; i < arguments.Length; i++) { if (objectArguments[i].GetType() != parameters[i].ParameterType) { perfectMatch = false; break; } } if (perfectMatch) { yield return method; yield break; } } var candidates = new List<MethodBase>(); foreach (var method in methods) { var parameters = new object[arguments.Length]; try { for (var i = 0; i < arguments.Length; i++) { parameters[i] = engine.Options.GetTypeConverter().Convert( arguments[i].ToObject(), method.GetParameters()[i].ParameterType, CultureInfo.InvariantCulture); } } catch { // ignore method } candidates.Add(method); } foreach (var candidate in candidates) yield return candidate; }