Update() public method

Creates a new expression that is like this one, but using the supplied children. If all of the children are the same, it will return this expression.
public Update ( Expression @object, IEnumerable arguments ) : MethodCallExpression
@object Expression
arguments IEnumerable The property of the result.
return MethodCallExpression
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            var instanceType = node.Object == null ? null : node.Object.Type;

            var map = new[] { new { Param = instanceType, Arg = node.Object } }.ToList();
            map.AddRange(node.Method.GetParameters()
                .Zip(node.Arguments, (p, a) => new { Param = p.ParameterType, Arg = a }));

            // for any local collection parameters in the method, make a
            // replacement argument which will print its elements
            var replacements = (map.Where(x => x.Param != null && x.Param.IsGenericType)
                .Select(x => new {x, g = x.Param.GetGenericTypeDefinition()})
                .Where(@t => @t.g == typeof (IEnumerable<>) || @t.g == typeof (List<>))
                .Where(@t => @t.x.Arg.NodeType == ExpressionType.Constant)
                .Select(@t => new {@t, elementType = @t.x.Param.GetGenericArguments().Single()})
                .Select(
                    @t =>
                        new
                        {
                            @[email protected],
                            Replacement =
                                Expression.Constant("{" + string.Join("|", (IEnumerable) ((ConstantExpression) @[email protected]).Value) + "}")
                        })).ToList();

            if (replacements.Any())
            {
                var args = map.Select(x => (from r in replacements
                                            where r.Arg == x.Arg
                                            select r.Replacement).SingleOrDefault() ?? x.Arg).ToList();

                node = node.Update(args.First(), args.Skip(1));
            }

            return base.VisitMethodCall(node);
        }
        protected override Expression VisitMethodCall(MethodCallExpression methodCallExpression)
        {
            var newMethodCallExpression = (MethodCallExpression)base.VisitMethodCall(methodCallExpression);

            if (EntityQueryModelVisitor.IsPropertyMethod(methodCallExpression.Method))
            {
                var subQueryExpression = newMethodCallExpression.Arguments[0] as SubQueryExpression;
                var subSelector = subQueryExpression?.QueryModel.SelectClause.Selector as QuerySourceReferenceExpression;

                if (subSelector != null)
                {
                    var subQueryModel = subQueryExpression.QueryModel;

                    subQueryModel.SelectClause.Selector
                        = methodCallExpression
                            .Update(
                                null,
                                new[]
                                {
                                    subSelector,
                                    methodCallExpression.Arguments[1]
                                });

                    subQueryModel.ResultTypeOverride = subQueryModel.SelectClause.Selector.Type;

                    return new SubQueryExpression(subQueryModel);
                }
            }

            return newMethodCallExpression;
        }
        /// <summary>
        /// Visits a method call expression and visits the <see cref="MethodCallExpression.Object"/> of a value
        /// type using <see cref="VisitLval"/> and <see cref="MethodCallExpression.Arguments"/> that are passed
        /// to <c>ref</c> or <c>out</c> parameters using <see cref="VisitLval"/>.
        /// </summary>
        /// <param name="node">The method call expression to visit.</param>
        /// <returns>The result of visiting the method call expression.</returns>
        protected virtual Expression VisitMethodCallCore(MethodCallExpression node)
        {
            var oldObject = node.Object;
            var newObject = IsValueInstance(oldObject) ? VisitLval(oldObject) : Visit(oldObject);

            var parameters = node.Method.GetParameters();

            var arguments = VisitArguments(node.Arguments, parameters);

            return(node.Update(newObject, arguments));
        }
Esempio n. 4
0
        protected override ExpressionTree VisitMethodCall(MethodCallExpression node)
        {
            MethodCallExpression expression;

            if (node.Object != null)
            {
                CheckChildrenCount(1 + node.Arguments.Count);

                var o = ExtractChildExpression(0);
                var a = ExtractChildExpressions(1, node.Arguments.Count);

                expression = node.Update(o, a);
            }
            else
            {
                CheckChildrenCount(node.Arguments.Count);

                var a = ExtractChildExpressions(0, node.Arguments.Count);

                expression = node.Update(@object: null, a);
            }

            return(CreateExpressionTree(expression));
        }
		protected override Expression VisitMethodCall(MethodCallExpression node)
		{
			if (node.Method.Name == "Where")
			{
				var lambda = (LambdaExpression)node.Arguments[1].StripQuotes();
				var predicate = lambda.Body.StripQuotes();

				if (CanMakeEqualTrueFalse(predicate))
				{
					var newLambda = Expression.Lambda(MakeEqualTrueFalse(predicate), lambda.Parameters);

					return base.VisitMethodCall(node.Update(null, new[] { node.Arguments[0], newLambda }));
				}
			}

			return base.VisitMethodCall(node);
		}
		protected override Expression VisitMethodCall(MethodCallExpression node)
		{
			if (node.Method.Name == "Where")
			{
				var innerMethodCall = node.Arguments[0] as MethodCallExpression;
				if (innerMethodCall != null && innerMethodCall.Method.Name == "Where")
				{
					var currentLambda = (LambdaExpression)node.Arguments[1].StripQuotes();
					var innerLambda = (LambdaExpression)innerMethodCall.Arguments[1].StripQuotes();

					if (currentLambda.Type != innerLambda.Type)
					{
						throw new NotSupportedException("Where methods have predicates with mismatch return type or arguments list");
					}

					var newCondition = Expression.AndAlso(innerLambda.Body, currentLambda.Body);
					var newLambda = Expression.Lambda(newCondition, currentLambda.Parameters);

					return Visit(node.Update(null, new[] { innerMethodCall.Arguments[0], newLambda }));
				}
			}
			return base.VisitMethodCall(node);
		}
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            if (node.Method.Name == "Take")
            {
                this.criteria = new SearchCriteria();
            }
            else if (node.Method.Name == "Where")
            {
                Type[] genericArguments = node.Method.GetGenericArguments();
                if (genericArguments.Length == 1 && genericArguments[0] == typeof(Person))
                {
                    return node.Update(
                        node.Object,
                        new Expression[]
                            {
                                Visit(node.Arguments[0]),
                                Expression.Quote(VisitWhereClause((LambdaExpression)((UnaryExpression)(node.Arguments[1])).Operand))
                            });

                }
            }

            return base.VisitMethodCall(node);
        }
Esempio n. 8
0
        private static MethodCallExpression Update(MethodCallExpression node)
        {
            // Tests the call of Update to Expression.Call factories.

            var res = node.Update(node.Object, node.Arguments.ToArray());

            Assert.NotSame(node, res);

            return res;
        }
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            var children = new[] {node.Object}.Concat(node.Arguments).ToList();

            VisitChildren(children);

            return node.Update(children[0], children.Skip(1));
        }