private Expression GenerateMethodCall(MethodData method, ArgumentData target, IList <ArgumentData> args) { args = BindingReflectionExtensions.GetMethodArgs(method.IsExtensionMethod, target, args); var expressions = new Expression[args.Count]; for (int index = 0; index < args.Count; index++) { var data = args[index]; if (data.IsLambda) { _lambdaParameter = method.Parameters[index]; data.UpdateExpression(BuildExpression(data.Node)); _lambdaParameter = null; } expressions[index] = data.Expression; } var methodInfo = method.Build(args); expressions = ConvertParameters(methodInfo, expressions); if (method.IsExtensionMethod) { return(Expression.Call(null, methodInfo, expressions)); } return(Expression.Call(target.Expression, methodInfo, expressions)); }
private Expression TryGenerateMethodCall(IList <MethodData> methods, ArgumentData target, IList <ArgumentData> arguments) { if (methods == null || methods.Count == 0) { return(null); } var methodInfos = new List <MethodInfo>(methods.Count); var methodArgs = new List <Expression[]>(methods.Count); for (int i = 0; i < methods.Count; i++) { try { var method = methods[i]; var args = BindingReflectionExtensions.GetMethodArgs(method.IsExtensionMethod, target, arguments); var expressions = new Expression[args.Count]; for (int index = 0; index < args.Count; index++) { var data = args[index]; if (data.IsLambda) { _lambdaParameter = method.Parameters[index]; data.UpdateExpression(BuildExpression(data.Node)); _lambdaParameter = null; } expressions[index] = data.Expression; } var methodInfo = method.Build(args); if (methodInfo != null) { methodInfos.Add(methodInfo); methodArgs.Add(expressions); } } catch { ; } } bool resultHasParams; var resultIndex = TrySelectMethod(methodInfos, methodArgs, (i, args) => args[i].ToArrayEx(e => e.Type), out resultHasParams); if (resultIndex < 0) { return(null); } var result = methodInfos[resultIndex]; var resultArgs = methodArgs[resultIndex]; var resultParameters = result.GetParameters(); resultArgs = ConvertParameters(resultParameters, resultArgs, resultHasParams); return(Expression.Call(result.IsExtensionMethod() ? null : target.Expression, result, resultArgs)); }