Exemplo n.º 1
0
        internal static string GetAssignment(
            string target,
            ExpressionType assignmentType,
            Expression value,
            TranslationContext context)
        {
            var symbol = _symbolsByNodeType[assignmentType];

            var valueString = (value.NodeType == ExpressionType.Default)
                ? DefaultExpressionTranslator.Translate((DefaultExpression)value)
                : GetValueTranslation(value, context);

            var assignment = target + " " + symbol;

            if (!valueString.StartsWithNewLine())
            {
                assignment += " ";
            }

            assignment += valueString;

            assignment = AdjustForCheckedAssignmentIfAppropriate(assignmentType, assignment);

            return(assignment);
        }
        private ConfiguredLambdaInfo GetValueLambdaInfo(Type targetValueType)
        {
            if (_customValueLambdaInfo != null)
            {
                return(_customValueLambdaInfo);
            }
#if NET35
            var            customValueLambda = _customValueLambda.ToDlrExpression();
            const ExprType CONSTANT          = ExprType.Constant;
#else
            var            customValueLambda = _customValueLambda;
            const ExprType CONSTANT          = Constant;
#endif
            if ((customValueLambda.Body.NodeType != CONSTANT) ||
                (targetValueType == typeof(object)) ||
                customValueLambda.ReturnType.IsAssignableTo(targetValueType) ||
                _configInfo.HasTargetMemberMatcher())
            {
                return(_customValueLambdaInfo = ConfiguredLambdaInfo.For(customValueLambda, _configInfo));
            }

            var convertedConstantValue = MapperContext
                                         .GetValueConversion(customValueLambda.Body, targetValueType);

            var funcType            = Expr.GetFuncType(targetValueType);
            var valueLambda         = Expr.Lambda(funcType, convertedConstantValue);
            var valueFunc           = valueLambda.Compile();
            var value               = valueFunc.DynamicInvoke().ToConstantExpression(targetValueType);
            var constantValueLambda = Expr.Lambda(funcType, value);
            var valueLambdaInfo     = ConfiguredLambdaInfo.For(constantValueLambda, _configInfo);

            return(_customValueLambdaInfo = valueLambdaInfo);
        }
Exemplo n.º 3
0
        private static string Translate(ExpressionType negationType, Expression expression, TranslationContext context)
        {
            var valueToNegate = context.Translate(expression);

            if (WrapNegatedValue(valueToNegate, expression))
            {
                valueToNegate = valueToNegate.WithSurroundingParentheses();
            }

            return _negationsByNodeType[negationType] + valueToNegate;
        }
            public StandaloneBoolean(
                Expression boolean,
                ExpressionType @operator,
                Expression comparison)
            {
                Boolean = boolean;

                var comparisonValue =
                    (comparison.NodeType != ExpressionType.Default) &&
                    (bool)((ConstantExpression)comparison).Value;

                IsComparisonToTrue =
                    (comparisonValue && (@operator == ExpressionType.Equal)) ||
                    (!comparisonValue && (@operator == ExpressionType.NotEqual));
            }
Exemplo n.º 5
0
        private static string AdjustForCheckedAssignmentIfAppropriate(
            ExpressionType assignmentType,
            string assignment)
        {
            if (Array.IndexOf(_checkedAssignmentTypes, assignmentType) == -1)
            {
                return(assignment);
            }

            if (assignment.IsMultiLine())
            {
                return($@"
checked
{{
{assignment.Indented()}
}}".TrimStart());
            }

            return($"checked {{ {assignment} }}");
        }
        private static string AdjustForCheckedOperatorIfAppropriate(
            ExpressionType operatorType,
            string operation)
        {
            if (Array.IndexOf(_checkedOperatorTypes, operatorType) == -1)
            {
                return(operation.WithSurroundingParentheses());
            }

            if (operation.IsMultiLine())
            {
                return($@"
checked
{{
{operation.TrimStart().Indented()}
}}".TrimStart());
            }

            return($"checked({operation})");
        }