public Expression Transform(NewExpression expression)
        {
            if (expression.Members == null && CanAddMembers(expression.Type, expression.Arguments))
            {
                var members = GetMembers(expression.Constructor, expression.Arguments);
                return(Expression.New(
                           expression.Constructor,
                           ExpressionTreeVisitor.AdjustArgumentsForNewExpression(expression.Arguments, members),
                           members));
            }

            return(expression);
        }
示例#2
0
        public void AdjustArgumentsForNewExpression()
        {
            var arguments = new[] { Expression.Constant(0), Expression.Constant("string1"), Expression.Constant("string2") };
            var tupleType = typeof(Tuple <double, object, string>);
            var members   = new MemberInfo[] { tupleType.GetProperty("Item1"), tupleType.GetMethod("get_Item2"), tupleType.GetProperty("Item3") };

            var result = ExpressionTreeVisitor.AdjustArgumentsForNewExpression(arguments, members).ToArray();

            Assert.That(result.Length, Is.EqualTo(3));
            var expected1 = Expression.Convert(arguments[0], typeof(double));

            ExpressionTreeComparer.CheckAreEqualTrees(expected1, result[0]);
            var expected2 = Expression.Convert(arguments[1], typeof(object));

            ExpressionTreeComparer.CheckAreEqualTrees(expected2, result[1]);
            Assert.That(result[2], Is.SameAs(arguments[2]));
        }
        public Expression Transform(NewExpression expression)
        {
            ArgumentUtility.CheckNotNull("expression", expression);

// ReSharper disable ConditionIsAlwaysTrueOrFalse
// ReSharper disable HeuristicUnreachableCode
            if (expression.Members == null && CanAddMembers(expression.Type, expression.Arguments))
            {
                var members = GetMembers(expression.Constructor, expression.Arguments);
                return(Expression.New(
                           expression.Constructor,
                           ExpressionTreeVisitor.AdjustArgumentsForNewExpression(expression.Arguments, members),
                           members));
            }
// ReSharper restore HeuristicUnreachableCode
// ReSharper restore ConditionIsAlwaysTrueOrFalse

            return(expression);
        }