コード例 #1
0
        public void AdjustArgumentsForNewExpression_WithMemberCountMismatch_ThrowsArgumentException()
        {
            var arguments = new[] { Expression.Constant(0), Expression.Constant("string1") };
            var tupleType = typeof(Tuple <double, object>);
            var members   = new MemberInfo[] { tupleType.GetProperty("Item1") };

            Assert.That(
                () => RelinqExpressionVisitor.AdjustArgumentsForNewExpression(arguments, members).ToArray(),
                Throws.ArgumentException.With.Message.EqualTo("Incorrect number of arguments for the given members."));
        }
コード例 #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 = RelinqExpressionVisitor.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]));
        }
コード例 #3
0
        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,
                           RelinqExpressionVisitor.AdjustArgumentsForNewExpression(expression.Arguments, members),
                           members));
            }
// ReSharper restore HeuristicUnreachableCode
// ReSharper restore ConditionIsAlwaysTrueOrFalse

            return(expression);
        }