public override object Invoke(Type[] types, object[] parameters, out Type returnType) { MethodInfo methodInfo = GetMethodInfo(types); if (methodInfo == null) { throw new MissingMethodException(MethodName); } returnType = methodInfo.ReturnType; return(LazyBinder.Invoke(methodInfo, _object, parameters)); }
public override ValueExpression Evaluate(IParserContext context) { object methodObject = MethodExpression.Evaluate(context).Value; ValueExpression[] parameters = EvaluateExpressionArray(Parameters, context); Type[] parameterTypes = parameters.ConvertAll(expr => expr.Type); object[] parameterValues = parameters.ConvertAll(expr => expr.Value); if (methodObject is MethodDefinition) { Type returnType; return(Exp.Value(TokenPosition, ((MethodDefinition)methodObject).Invoke(parameterTypes, parameterValues, out returnType), returnType)); } if (methodObject is ConstructorInfo[]) { ConstructorInfo[] constructors = (ConstructorInfo[])methodObject; MethodBase method = LazyBinder.SelectBestMethod(constructors, parameterTypes, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); if (method == null) { throw new ExpressionEvaluationException("No match found for constructor " + constructors[0].Name, this); } object value = LazyBinder.Invoke(method, parameterValues); //object value = ((ConstructorInfo)method).Invoke(parameterValues); return(Exp.Value(TokenPosition, value, method.DeclaringType)); } if (methodObject is Delegate[]) { Delegate[] delegates = (Delegate[])methodObject; MethodBase[] methods = delegates.ConvertAll <Delegate, MethodBase>(d => d.GetMethodInfo()); MethodBase method = LazyBinder.SelectBestMethod(methods, parameterTypes, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); if (method == null) { throw new ExpressionEvaluationException("No match found for delegate " + MethodExpression, this); } object value = LazyBinder.Invoke(method, delegates[Array.IndexOf(methods, method)].Target, parameterValues); return(Exp.Value(TokenPosition, value, ((MethodInfo)method).ReturnType)); } if (methodObject is Delegate) { Delegate method = (Delegate)methodObject; MethodInfo methodInfo = method.GetMethodInfo(); object value = methodInfo.Invoke(method.Target, parameterValues); return(new ValueExpression(TokenPosition, value, methodInfo.ReturnType)); } if (methodObject is FunctionDefinitionExpression) { FunctionDefinitionExpression func = (FunctionDefinitionExpression)methodObject; var functionContext = context.CreateLocal(); for (int i = 0; i < parameterValues.Length; i++) { functionContext.Set(func.ParameterNames[i], parameterValues[i]); } return(func.Body.Evaluate(functionContext)); } throw new ExpressionEvaluationException(MethodExpression + " is not a function", this); }