Пример #1
0
        private static Expression UseLocalValueVariable(
            ParameterExpression variable,
            Expression variableValue,
            Expression body,
            bool performValueReplacement = false)
        {
            var variableAssignment = variable.AssignTo(variableValue);

            if (body.NodeType != ExpressionType.Try)
            {
                if (performValueReplacement)
                {
                    body = body.Replace(variableValue, variable);
                }

                return(Expression.Block(new[] { variable }, variableAssignment, body));
            }

            var tryCatch = (TryExpression)body;

            body = tryCatch.Update(
                Expression.Block(variableAssignment, tryCatch.Body.Replace(variableValue, variable)),
                tryCatch.Handlers,
                tryCatch.Finally,
                tryCatch.Fault);

            return(Expression.Block(new[] { variable }, body));
        }
Пример #2
0
        public static LoopExpression InsertAssignment(
            this LoopExpression loop,
            int insertIndex,
            ParameterExpression variable,
            Expression value)
        {
            var loopBody            = (BlockExpression)loop.Body;
            var loopBodyExpressions = new Expression[loopBody.Expressions.Count + 1];
            var expressionOffset    = 0;

            for (var i = 0; i < loopBodyExpressions.Length; i++)
            {
                if (i != insertIndex)
                {
                    loopBodyExpressions[i] = loopBody.Expressions[i - expressionOffset];
                    continue;
                }

                loopBodyExpressions[i] = variable.AssignTo(value);
                expressionOffset       = 1;
            }

            loopBody = loopBody.Update(loopBody.Variables.Append(variable), loopBodyExpressions);

            return(loop.Update(loop.BreakLabel, loop.ContinueLabel, loopBody));
        }
Пример #3
0
        private static Expression UseLocalValueVariable(
            ParameterExpression variable,
            Expression variableValue,
            Expression body,
            IMemberMapperData mapperData,
            bool performValueReplacement = false)
        {
            var variableAssignment = variable.AssignTo(variableValue);

            if (body.NodeType == ExpressionType.Block)
            {
                if (performValueReplacement)
                {
                    body = body.Replace(variableValue, variable);
                }

                var block = (BlockExpression)body;

                return(Expression.Block(
                           block.Variables.Append(variable),
                           block.Expressions.Prepend(variableAssignment)));
            }

            var tryCatch = (body.NodeType != ExpressionType.Try)
                ? body.WrapInTryCatch(mapperData)
                : (TryExpression)body;

            body = tryCatch.Update(
                Expression.Block(variableAssignment, tryCatch.Body.Replace(variableValue, variable)),
                tryCatch.Handlers,
                tryCatch.Finally,
                tryCatch.Fault);

            return(Expression.Block(new[] { variable }, body));
        }
        public BlockExpression GetLoopBlock(
            Expression loop,
            Func <Expression, Expression> enumeratorValueFactory = null,
            Func <Expression, Expression> finallyClauseFactory   = null)
        {
            Expression enumeratorValue = Expression.Call(_enumerableSubject, _getEnumeratorMethod);

            if (enumeratorValueFactory != null)
            {
                enumeratorValue = enumeratorValueFactory.Invoke(enumeratorValue);
            }

            var enumeratorAssignment = _enumerator.AssignTo(enumeratorValue);

            Expression finallyClause = Expression.Call(
                _enumerator,
                typeof(IDisposable).GetPublicInstanceMethod("Dispose"));

            if (finallyClauseFactory != null)
            {
                finallyClause = finallyClauseFactory.Invoke(finallyClause);
            }

            return(Expression.Block(
                       new[] { _enumerator },
                       enumeratorAssignment,
                       Expression.TryFinally(loop, finallyClause)));
        }
        private Expression GetUntypedEnumerableAssignment(out ParameterExpression enumerableVariable)
        {
            enumerableVariable = Expression.Variable(typeof(IEnumerable), "sourceEnumerable");
            var sourceValue          = _instanceDictionaryAdapter.GetEntryValueAccess();
            var valueAsEnumerable    = Expression.TypeAs(sourceValue, typeof(IEnumerable));
            var enumerableAssignment = enumerableVariable.AssignTo(valueAsEnumerable);

            return(enumerableAssignment);
        }
Пример #6
0
        public Expression Adapt(LoopExpression loop)
        {
            var sourceEnumerableFoundTest   = GetSourceEnumerableFoundTest(_emptyTarget, _builder);
            var assignSourceEnumerableFound = (Expression)_sourceEnumerableFound.AssignTo(sourceEnumerableFoundTest);

            var adaptedLoop = _elementsDictionaryLoopData.Adapt(loop);

            var enumerableLoopBlock = _enumerableLoopData.GetLoopBlock(
                adaptedLoop,
                GetEnumeratorIfNecessary,
                DisposeEnumeratorIfNecessary);

            return(Expression.Block(
                       enumerableLoopBlock.Variables.Append(_sourceEnumerableFound),
                       enumerableLoopBlock.Expressions.Prepend(assignSourceEnumerableFound)));
        }
        private Expression GetNonSimpleElementLoopExitCheck(Func <Expression> containsKeyElementCallFactory)
        {
            var noKeysStartWithTarget = GetNoKeysWithMatchingStartQuery();

            if (DoNotPerformElementChecks)
            {
                return(noKeysStartWithTarget);
            }

            var containsElementKeyCall = containsKeyElementCallFactory.Invoke();

            _elementKeyExists = Expression.Variable(typeof(bool), "elementKeyExists");
            var assignElementKeyExists = _elementKeyExists.AssignTo(containsElementKeyCall);
            var elementKeyDoesNotExist = Expression.Not(assignElementKeyExists);

            return(Expression.AndAlso(elementKeyDoesNotExist, noKeysStartWithTarget));
        }
        public static LoopExpression InsertAssignment(
            this LoopExpression loop,
            int insertIndex,
            ParameterExpression variable,
            Expression value)
        {
            var loopBody            = (BlockExpression)loop.Body;
            var loopBodyExpressions = new List <Expression>(loopBody.Expressions);

            var variableAssignment = variable.AssignTo(value);

            loopBodyExpressions.Insert(insertIndex, variableAssignment);

            loopBody = loopBody.Update(loopBody.Variables.Append(variable), loopBodyExpressions);

            return(loop.Update(loop.BreakLabel, loop.ContinueLabel, loopBody));
        }