Пример #1
0
        public static bool TryConvert(
            BinaryExpression binary,
            IQueryProjectionModifier modifier,
            out Expression converted)
        {
            if ((binary.NodeType != ExpressionType.Add) ||
                !ReferenceEquals(binary.Method, _stringConcatObjectsMethod) ||
                ((binary.Left.NodeType != ExpressionType.Convert) && (binary.Right.NodeType != ExpressionType.Convert)))
            {
                converted = null;
                return(false);
            }

            var convertedLeft  = ConvertOperand(binary.Left, modifier);
            var convertedRight = ConvertOperand(binary.Right, modifier);

            if ((convertedLeft == binary.Left) && (convertedRight == binary.Right))
            {
                converted = null;
                return(false);
            }

            if ((convertedLeft == null) || (convertedRight == null))
            {
                converted = convertedLeft ?? convertedRight;
                return(true);
            }

            converted = Expression.Add(convertedLeft, convertedRight, _stringConcatStringsMethod);
            return(true);
        }
Пример #2
0
        private static Expression ConvertOperand(Expression value, IQueryProjectionModifier modifier)
        {
            while (true)
            {
                switch (value.NodeType)
                {
                case ExpressionType.Constant:
                    var constant = ((ConstantExpression)value).Value;

                    return((constant == null)
                            ? null
                            : (value.Type != typeof(string))
                                ? constant.ToString().ToConstantExpression()
                                : ((string)constant != string.Empty) ? value : null);

                case ExpressionType.Convert:
                    var conversion = (UnaryExpression)value;

                    if ((conversion.Operand.NodeType == ExpressionType.Constant))
                    {
                        value = conversion.Operand;
                        continue;
                    }

                    return(conversion.Operand.Type == typeof(string)
                            ? conversion.Operand
                            : modifier.Modify(modifier
                                              .MapperData
                                              .GetValueConversion(conversion.Operand, typeof(string))));

                default:
                    return(value);
                }
            }
        }
Пример #3
0
 private static bool ShouldConvert(
     MemberAssignment assignment,
     IQueryProjectionModifier modifier)
 {
     return(modifier.Settings.SupportsEnumerableMaterialisation &&
            (assignment.Expression.NodeType == ExpressionType.Call) &&
            IsLinqSelectCall((MethodCallExpression)assignment.Expression));
 }
Пример #4
0
        private static MemberAssignment ConvertToMaterialisation(
            MemberAssignment assignment,
            IQueryProjectionModifier modifier)
        {
            var materialisedNestedProjection = GetMaterialisedNestedProjection(assignment.Expression);
            var modifiedProjection           = modifier.Modify(materialisedNestedProjection);

            return(assignment.Update(modifiedProjection));
        }
Пример #5
0
        private static MethodCallExpression AdjustForFormatStringIfNecessary(
            MethodCallExpression toStringCall,
            IQueryProjectionModifier modifier)
        {
            if (modifier.Settings.SupportsToStringWithFormat || toStringCall.Arguments.None())
            {
                return(toStringCall);
            }

            return(toStringCall.Object.WithToStringCall());
        }
Пример #6
0
        public static bool TryConvert(
            ConditionalExpression conditional,
            IQueryProjectionModifier modifier,
            out Expression converted)
        {
            if (modifier.Settings.SupportsStringToEnumConversion || IsNotStringToEnumConversion(conditional))
            {
                converted = null;
                return false;
            }

            converted = modifier.Settings.ConvertStringToEnumConversion(conditional);
            return true;
        }
Пример #7
0
        private static Expression GetNullCheckedEnumToStringCall(
            MethodCallExpression toStringCall,
            Type subjectNonNullableType,
            IQueryProjectionModifier modifier)
        {
            if (modifier.Settings.SupportsEnumToStringConversion)
            {
                return(GetNullCheckedToStringCall(toStringCall, subjectNonNullableType));
            }

            return(GetNullCheckedToStringCall(
                       toStringCall.Object,
                       Enum.GetUnderlyingType(subjectNonNullableType)));
        }
Пример #8
0
        private static Expression GetNullCheckedToStringCall(
            MethodCallExpression toStringCall,
            IQueryProjectionModifier modifier)
        {
            // ReSharper disable once PossibleNullReferenceException
            var subjectNonNullableType = toStringCall.Object.Type.GetNonNullableType();

            if (subjectNonNullableType.IsEnum())
            {
                return(GetNullCheckedEnumToStringCall(toStringCall, subjectNonNullableType, modifier));
            }

            return(GetNullCheckedToStringCall(toStringCall, subjectNonNullableType));
        }
Пример #9
0
        public static bool TryConvert(
            MemberAssignment assignment,
            IQueryProjectionModifier modifier,
            out MemberAssignment converted)
        {
            if (ShouldConvert(assignment, modifier))
            {
                converted = ConvertToMaterialisation(assignment, modifier);
                return(true);
            }

            converted = null;
            return(false);
        }
Пример #10
0
        public static bool TryConvert(
            ConditionalExpression conditional,
            IQueryProjectionModifier modifier,
            out Expression converted)
        {
            if (modifier.Settings.SupportsNonEntityNullConstants || !conditional.Type.IsComplex())
            {
                converted = null;
                return(false);
            }

            converted = new NonNullableMemberBinder(conditional).GuardMemberAccesses();
            return(true);
        }
Пример #11
0
        public static bool TryConvert(
            MethodCallExpression methodCall,
            IQueryProjectionModifier modifier,
            out Expression converted)
        {
            if (modifier.Settings.SupportsStringEqualsIgnoreCase || IsNotEqualsIgnoreCaseCall(methodCall))
            {
                converted = null;
                return(false);
            }

            converted = modifier.Settings.ConvertStringEqualsIgnoreCase(methodCall);
            converted = modifier.Modify(converted);
            return(true);
        }
Пример #12
0
        public static bool TryConvert(
            BinaryExpression comparison,
            IQueryProjectionModifier modifier,
            out Expression converted)
        {
            if (modifier.Settings.SupportsComplexTypeToNullComparison ||
                (comparison.Left.Type == typeof(object)) ||
                !comparison.Left.Type.IsComplex())
            {
                converted = null;
                return(false);
            }

            converted = Convert(comparison, modifier);
            return(converted != null);
        }
Пример #13
0
        private static Expression GetReturnedValue(
            Expression returnExpression,
            IQueryProjectionModifier modifier)
        {
            switch (returnExpression.NodeType)
            {
            case Label:
                returnExpression = ((LabelExpression)returnExpression).DefaultValue;
                break;

            case Goto:
                returnExpression = ((GotoExpression)returnExpression).Value;
                break;
            }

            return(modifier.Modify(returnExpression));
        }
        public static bool TryConvert(
            MethodCallExpression methodCall,
            IQueryProjectionModifier modifier,
            out Expression converted)
        {
            if (modifier.Settings.SupportsGetValueOrDefault || IsNotGetValueOrDefaultCall(methodCall))
            {
                converted = null;
                return(false);
            }

            // ReSharper disable once AssignNullToNotNullAttribute
            converted = Expression.Condition(
                methodCall.Object.GetIsNotDefaultComparison(),
                Expression.Convert(methodCall.Object, methodCall.Type),
                DefaultValueConstantExpressionFactory.CreateFor(methodCall));

            return(true);
        }
Пример #15
0
        public static bool TryConvert(
            MemberAssignment assignment,
            IQueryProjectionModifier modifier,
            out MemberAssignment converted)
        {
            if (assignment.Expression.NodeType != ExpressionType.Block)
            {
                converted = null;
                return(false);
            }

            var valueBlock      = (BlockExpression)assignment.Expression;
            var finalExpression = valueBlock.Expressions.Last();

            if (finalExpression.NodeType != ExpressionType.Conditional)
            {
                converted = null;
                return(false);
            }

            var tryParseOrDefault = (ConditionalExpression)finalExpression;

            if ((tryParseOrDefault.Test.NodeType != ExpressionType.Call) ||
                (tryParseOrDefault.IfTrue.NodeType != ExpressionType.Parameter))
            {
                converted = null;
                return(false);
            }

            var methodCall = (MethodCallExpression)tryParseOrDefault.Test;

            if (!methodCall.Method.IsStatic || (methodCall.Method.Name != nameof(int.TryParse)))
            {
                converted = null;
                return(false);
            }

            var convertedValue = modifier.Settings.ConvertTryParseCall(methodCall, tryParseOrDefault.IfFalse);

            converted = assignment.Update(convertedValue);
            return(true);
        }
Пример #16
0
        public static bool TryConvert(
            BlockExpression mappingBlock,
            IQueryProjectionModifier modifier,
            out Expression converted)
        {
            if (IsNotDerivedTypeMappingsBlock(mappingBlock, out var derivedTypeMappings))
            {
                converted = null;
                return(false);
            }

            var fallbackValue = GetReturnedValue(mappingBlock.Expressions.Last(), modifier);

            converted = derivedTypeMappings.Reverse().Aggregate(
                fallbackValue,
                (mappingSoFar, derivedTypeMapping) => Expression.Condition(
                    derivedTypeMapping.Test,
                    GetReturnedValue(derivedTypeMapping.IfTrue, modifier).GetConversionTo(fallbackValue.Type),
                    mappingSoFar));

            return(true);
        }
Пример #17
0
        private static Expression Convert(BinaryExpression comparison, IQueryProjectionModifier modifier)
        {
            if (!modifier.MapperData.IsEntity(comparison.Left.Type, out var idMember))
            {
                return(null);
            }

            var entityMemberAccess = comparison.Left;
            var entityParentAccess = entityMemberAccess.GetParentOrNull();

            if (TryGetEntityMemberIdMemberOrNull(
                    entityParentAccess,
                    entityMemberAccess,
                    idMember,
                    out var entityMemberIdMember))
            {
                return(entityMemberIdMember
                       .GetAccess(entityParentAccess)
                       .GetIsNotDefaultComparison());
            }

            return(null);
        }
Пример #18
0
        public static bool TryConvert(
            MethodCallExpression methodCall,
            IQueryProjectionModifier modifier,
            out Expression converted)
        {
            if (IsNotToStringCall(methodCall))
            {
                converted = null;
                return(false);
            }

            if (modifier.Settings.SupportsToString)
            {
                var convertedCall = AdjustForFormatStringIfNecessary(methodCall, modifier);
                converted = GetNullCheckedToStringCall(convertedCall, modifier);

                return((convertedCall != methodCall) || (converted != convertedCall));
            }

            methodCall = AdjustForFormatStringIfNecessary(methodCall, modifier);
            converted  = modifier.Settings.ConvertToStringCall(methodCall);
            return(true);
        }
        public static bool TryConvert(
            MemberAssignment assignment,
            IQueryProjectionModifier modifier,
            out MemberAssignment converted)
        {
            if (assignment.Expression.NodeType != ExpressionType.Convert)
            {
                converted = null;
                return(false);
            }

            var conversion = (UnaryExpression)assignment.Expression;

            if (!conversion.Operand.Type.IsNullableType())
            {
                converted = null;
                return(false);
            }

            var value = GetNullCheckedConversion(conversion.Operand, conversion);

            converted = assignment.Update(value);
            return(true);
        }