예제 #1
0
        public void UpdateTest()
        {
            SampleClassWithProperties instance = new SampleClassWithProperties
            {
                DefaultProperty = new List <int> {
                    100, 101
                },
                AlternativeProperty = new List <int> {
                    200, 201
                }
            };

            IndexExpression  expr        = instance.DefaultIndexExpression;
            MemberExpression newProperty = Expression.Property(Expression.Constant(instance),
                                                               typeof(SampleClassWithProperties).GetProperty(nameof(instance.AlternativeProperty)));

            ConstantExpression[] newArguments = { Expression.Constant(1) };

            IndexExpression exprUpdated = expr.Update(newProperty, newArguments);

            // Replace Object and Arguments of IndexExpression.
            IndexExpressionHelpers.AssertEqual(
                exprUpdated,
                Expression.MakeIndex(newProperty, instance.DefaultIndexer, newArguments));

            // Invoke to check expression.
            IndexExpressionHelpers.AssertInvokeCorrect(100, expr);
            IndexExpressionHelpers.AssertInvokeCorrect(201, exprUpdated);
        }
        public void RewriteObjectTest()
        {
            var instance = new SampleClassWithProperties
            {
                DefaultProperty = new List <int> {
                    100, 101
                },
                AlternativeProperty = new List <int> {
                    200, 201
                }
            };

            IndexExpression  expr        = instance.DefaultIndexExpression;
            MemberExpression newProperty = Expression.Property(Expression.Constant(instance),
                                                               typeof(SampleClassWithProperties).GetProperty(nameof(instance.AlternativeProperty)));

            var             visitor  = new IndexVisitor(expr, newProperty, expr.Arguments.ToArray());
            IndexExpression actual   = (IndexExpression)visitor.Visit(expr);
            IndexExpression expected = Expression.MakeIndex(newProperty, expr.Indexer, expr.Arguments);

            // Object of ExpressionIndex replaced via Rewrite method call.
            IndexExpressionHelpers.AssertEqual(expected, actual);

            // Invoke to check expression.
            IndexExpressionHelpers.AssertInvokeCorrect(100, expr);
            IndexExpressionHelpers.AssertInvokeCorrect(200, actual);
        }
        public void RewriteArgumentsTest()
        {
            var instance = new SampleClassWithProperties {
                DefaultProperty = new List <int> {
                    100, 101
                }
            };

            IndexExpression expr = instance.DefaultIndexExpression;

            Expression[] newArguments = { Expression.Constant(1) };

            var             visitor  = new IndexVisitor(expr, expr.Object, newArguments);
            IndexExpression expected = Expression.MakeIndex(expr.Object, expr.Indexer, newArguments);
            var             actual   = (IndexExpression)visitor.Visit(expr);

            IndexExpressionHelpers.AssertEqual(expected, actual);

            // Invoke to check expression.
            IndexExpressionHelpers.AssertInvokeCorrect(100, expr);
            IndexExpressionHelpers.AssertInvokeCorrect(101, actual);
        }