public void VisitUnknownNonExtensionExpression_Ignored()
        {
            var expression = new UnknownExpression(typeof(object));
            var result     = MultiReplacingExpressionVisitor.Replace(_mapping, expression);

            Assert.That(result, Is.SameAs(expression));
        }
        private static LambdaExpression CreateSelectorForSelectNode(
            LambdaExpression keySelector,
            LambdaExpression elementSelectorOrResultSelector,
            LambdaExpression resultSelectorOrNull)
        {
            var resultSelector          = GetResultSelector(elementSelectorOrResultSelector, resultSelectorOrNull);
            var optionalElementSelector = GetOptionalElementSelector(elementSelectorOrResultSelector, resultSelectorOrNull);

            // If there is an element selector, the element type will be that returned by the element selector. Otherwise, it will be the type flowing into
            // the key selector.
            var elementType  = optionalElementSelector != null ? optionalElementSelector.Body.Type : keySelector.Parameters[0].Type;
            var groupingType = typeof(IGrouping <,>).MakeGenericType(keySelector.Body.Type, elementType);
            var keyProperty  = groupingType.GetRuntimeProperty("Key");

            var groupParameter = Expression.Parameter(groupingType, "group");
            var keyExpression  = Expression.MakeMemberAccess(groupParameter, keyProperty);

            var expressionMapping = new Dictionary <Expression, Expression> (2)
            {
                { resultSelector.Parameters[1], groupParameter },
                { resultSelector.Parameters[0], keyExpression }
            };
            var bodyWithGroupingAndKeyReplaced = MultiReplacingExpressionVisitor.Replace(expressionMapping, resultSelector.Body);

            return(Expression.Lambda(bodyWithGroupingAndKeyReplaced, groupParameter));
        }
        public void IgnoresTree_WhenReplacedNodesDoNotExist()
        {
            var tree = ExpressionHelper.CreateLambdaExpression();

            var result = MultiReplacingExpressionVisitor.Replace(_mapping, tree);

            Assert.That(result, Is.SameAs(tree));
        }
        public void ReplacesGivenNode_ByReplacement()
        {
            var tree = _replacedNode2;

            var result = MultiReplacingExpressionVisitor.Replace(_mapping, tree);

            Assert.That(result, Is.SameAs(_replacementNode2));
        }
        public void ReplacesTreeParts()
        {
            var tree = Expression.MakeBinary(ExpressionType.Add, _replacedNode1, _replacedNode2);

            var result = MultiReplacingExpressionVisitor.Replace(_mapping, tree);

            var expectedResult = Expression.MakeBinary(ExpressionType.Add, _replacementNode1, _replacementNode2);

            ExpressionTreeComparer.CheckAreEqualTrees(expectedResult, result);
        }
        public void VisitExtensionExpression_DescendsIntoChildren()
        {
            var tree = new VBStringComparisonExpression(_replacedNode1, true);

            var result = MultiReplacingExpressionVisitor.Replace(_mapping, tree);

            var expected = new VBStringComparisonExpression(_replacementNode1, true);

            ExpressionTreeComparer.CheckAreEqualTrees(expected, result);
        }
Пример #7
0
        private Expression InlineLambdaExpression(LambdaExpression lambdaExpression, ReadOnlyCollection <Expression> arguments)
        {
            Assertion.DebugAssert(lambdaExpression.Parameters.Count == arguments.Count);

            var mapping = new Dictionary <Expression, Expression> (arguments.Count);

            var body = lambdaExpression.Body;

            for (int i = 0; i < lambdaExpression.Parameters.Count; i++)
            {
                mapping.Add(lambdaExpression.Parameters[i], arguments[i]);
            }

            return(MultiReplacingExpressionVisitor.Replace(mapping, body));
        }
        public void ReplacesTreePart_InSubQueries()
        {
            var replacedNode    = ExpressionHelper.CreateQueryable <Cook> ().Expression;
            var replacementNode = Expression.Constant(null, typeof(Cook[]));
            var mapping         = new Dictionary <Expression, Expression> {
                { replacedNode, replacementNode }
            };

            var subQueryMainFromClause = new MainFromClause("c", typeof(Cook), replacedNode);
            var subQuery = ExpressionHelper.CreateQueryModel(subQueryMainFromClause);

            var tree = new SubQueryExpression(subQuery);

            MultiReplacingExpressionVisitor.Replace(mapping, tree);

            Assert.That(subQueryMainFromClause.FromExpression, Is.SameAs(replacementNode));
        }
Пример #9
0
        public static Expression Resolve <TParameter1, TParameter2, TResult> (
            IQuerySource sourceToReference1,
            IQuerySource sourceToReference2,
            Expression <Func <TParameter1, TParameter2, TResult> > expressionToBeResolved)
        {
            ArgumentUtility.CheckNotNull("sourceToReference1", sourceToReference1);
            ArgumentUtility.CheckNotNull("sourceToReference2", sourceToReference2);
            ArgumentUtility.CheckNotNull("expressionToBeResolved", expressionToBeResolved);

            var expressionMapping = new Dictionary <Expression, Expression> (2)
            {
                { expressionToBeResolved.Parameters[0], new QuerySourceReferenceExpression(sourceToReference1) },
                { expressionToBeResolved.Parameters[1], new QuerySourceReferenceExpression(sourceToReference2) }
            };
            var result = MultiReplacingExpressionVisitor.Replace(expressionMapping, expressionToBeResolved.Body);

            return(result);
        }