System.Linq.Expressions.MethodCallExpression.Update C# (CSharp) Method

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
        public MethodCallExpression Update(Expression @object, IEnumerable<Expression> arguments)
        {
            if (@object == Object && arguments == Arguments)
            {
                return this;
            }
            return Expression.Call(@object, Method, arguments);
        }

Usage Example

Ejemplo n.º 1
0
        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);
        }
All Usage Examples Of System.Linq.Expressions.MethodCallExpression::Update