Exemple #1
0
        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, new LazyBinder()), returnType));
            }

            if (methodObject is ConstructorInfo[])
            {
                ConstructorInfo[] constructors = (ConstructorInfo[])methodObject;

                MethodBase method = new LazyBinder().SelectMethod(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance, constructors, parameterTypes, null);

                if (method == null)
                {
                    throw new ExpressionEvaluationException("No match found for constructor " + constructors[0].Name, this);
                }

                object value = ((ConstructorInfo)method).Invoke(parameterValues);

                return(Exp.Value(TokenPosition, value, method.ReflectedType));
            }

            if (methodObject is Delegate[])
            {
                Delegate[]   delegates = (Delegate[])methodObject;
                MethodBase[] methods   = delegates.ConvertAll <Delegate, MethodBase>(d => d.Method);

                MethodBase method = new LazyBinder().SelectMethod(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance, methods, parameterTypes, null);

                if (method == null)
                {
                    throw new ExpressionEvaluationException("No match found for delegate " + _methodExpression, this);
                }

                object value = method.Invoke(delegates[Array.IndexOf(methods, method)].Target, parameterValues);

                return(Exp.Value(TokenPosition, value, ((MethodInfo)method).ReturnType));
            }

            if (methodObject is Delegate)
            {
                Delegate method = (Delegate)methodObject;

                object value = method.Method.Invoke(method.Target, parameterValues);

                return(new ValueExpression(TokenPosition, value, method.Method.ReturnType));
            }

            throw new ExpressionEvaluationException(_methodExpression + " is not a function", this);
        }
        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, new LazyBinder()), returnType);
			}

			if (methodObject is ConstructorInfo[])
			{
				ConstructorInfo[] constructors = (ConstructorInfo[]) methodObject;

                MethodBase method = new LazyBinder().SelectMethod(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance, constructors, parameterTypes, null);

				if (method == null)
					throw new ExpressionEvaluationException("No match found for constructor " + constructors[0].Name, this);

				object value = ((ConstructorInfo)method).Invoke(parameterValues);

                return Exp.Value(TokenPosition, value, method.ReflectedType);
			}

			if (methodObject is Delegate[])
			{
				Delegate[] delegates = (Delegate[]) methodObject;
				MethodBase[] methods = delegates.ConvertAll<Delegate, MethodBase>(d => d.Method);

                MethodBase method = new LazyBinder().SelectMethod(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance, methods, parameterTypes, null);

				if (method == null)
					throw new ExpressionEvaluationException("No match found for delegate " + _methodExpression, this);

				object value = method.Invoke(delegates[Array.IndexOf(methods, method)].Target, parameterValues);

                return Exp.Value(TokenPosition, value, ((MethodInfo)method).ReturnType);
			}

            if (methodObject is Delegate)
            {
                Delegate method = (Delegate) methodObject;

                object value = method.Method.Invoke(method.Target, parameterValues);

                return new ValueExpression(TokenPosition, value, method.Method.ReturnType);
            }

            throw new ExpressionEvaluationException(_methodExpression + " is not a function", this);
        }