Predicates/SimpleParser/Helpers/ExpressionAutoCast.cs
/// <summary>
/// Add an Expression.Convert to the numeric expressions requiring the cast
/// </summary>
/// <param name="highestIntType">Type highest float int to impose as a minimum required cast</param>
/// <param name="highestFloatType">Type highest float type to impose as a minimum required cast</param>
/// <param name="highestDecimalType">Type highest decimal type to impose as a minimum required cast</param>
/// <param name="expressions">The list of expressions to evaluate</param>
/// <returns>A new list of the same expressions where each one has been casted, if needed, to the correct type</returns>
Add an Expression.Convert to the numeric expressions requiring the cast
Add an Expression . Convert to the numeric expressions requiring the cast
public static IList<Expression> ApplyNumericTypeConversion(Type highestIntType,
            Type highestFloatType, Type highestDecimalType, params Expression[] expressions)
        {
            IntPriority highestInt = GetIntPriority(highestIntType);
            FloatPriority highestFloat = GetFloatPriority(highestFloatType);
            DecimalPriority highestDecimal = GetDecimalPriority(highestDecimalType);
            Type intType = highestIntType;
            Type floatType = highestFloatType;
            Type decimalType = highestDecimalType;
            bool allEquals = true;
            foreach (var expression in expressions)
            {
                IntPriority currentInt = IntPriority.None;
                FloatPriority currentFloat = FloatPriority.None;
                DecimalPriority currentDecimal = DecimalPriority.None;
                currentInt = GetIntPriority(expression.Type);
                if (currentInt == IntPriority.None)
                {
                    currentFloat = GetFloatPriority(expression.Type);
                    if (currentFloat == FloatPriority.None)
                    {
                        currentDecimal = GetDecimalPriority(expression.Type);
                        if (currentDecimal == DecimalPriority.None)
                        {
                            Debug.WriteLine("LevelType> at least one expression was not a number");
                            return expressions;
                        }
                    }
                }
                highestInt = Max(highestInt, currentInt);
                highestFloat = Max(highestFloat, currentFloat);
                highestDecimal = Max(highestDecimal, currentDecimal);
                if (highestInt == currentInt)
                {
                    if (intType != expression.Type)
                    {
                        allEquals = false;
                        intType = expression.Type;
                    }
                }
                if (highestFloat == currentFloat)
                {
                    if (floatType != expression.Type)
                    {
                        allEquals = false;
                        floatType = expression.Type;
                    }
                }
                if (highestDecimal == currentDecimal)
                {
                    if (decimalType != expression.Type)
                    {
                        allEquals = false;
                        decimalType = expression.Type;
                    }
                }
            }
            if (allEquals)
            {
                return expressions;
            }
            if (highestDecimal != DecimalPriority.None)
            {
                if (highestFloat != FloatPriority.None)
                {
                    return CastTo(expressions, typeof(double));
                }
                return CastTo(expressions, typeof(decimal));
            }
            if (highestFloat == FloatPriority.None)
            {
                return CastTo(expressions, intType);
            }
            return CastTo(expressions, floatType);
        }
public static IList < Expression > ApplyNumericTypeConversion ( Type highestIntType , Type highestFloatType , Type highestDecimalType , params Expression [ ] expressions ) { IntPriority highestInt = GetIntPriority ( highestIntType ) ; FloatPriority highestFloat = GetFloatPriority ( highestFloatType ) ; DecimalPriority highestDecimal = GetDecimalPriority ( highestDecimalType ) ; Type intType = highestIntType ; Type floatType = highestFloatType ; Type decimalType = highestDecimalType ; bool allEquals = true ; foreach ( var expression in expressions ) { IntPriority currentInt = IntPriority . None ; FloatPriority currentFloat = FloatPriority . None ; DecimalPriority currentDecimal = DecimalPriority . None ; currentInt = GetIntPriority ( expression . Type ) ; if ( currentInt == IntPriority . None ) { currentFloat = GetFloatPriority ( expression . Type ) ; if ( currentFloat == FloatPriority . None ) { currentDecimal = GetDecimalPriority ( expression . Type ) ; if ( currentDecimal == DecimalPriority . None ) { Debug . WriteLine ( " " ) ; return expressions ; } } } highestInt = Max ( highestInt , currentInt ) ; highestFloat = Max ( highestFloat , currentFloat ) ; highestDecimal = Max ( highestDecimal , currentDecimal ) ; if ( highestInt == currentInt ) { if ( intType != expression . Type ) { allEquals = false ; intType = expression . Type ; } } if ( highestFloat == currentFloat ) { if ( floatType != expression . Type ) { allEquals = false ; floatType = expression . Type ; } } if ( highestDecimal == currentDecimal ) { if ( decimalType != expression . Type ) { allEquals = false ; decimalType = expression . Type ; } } } if ( allEquals ) { return expressions ; } if ( highestDecimal != DecimalPriority . None ) { if ( highestFloat != FloatPriority . None ) { return CastTo ( expressions , typeof ( double ) ) ; } return CastTo ( expressions , typeof ( decimal ) ) ; } if ( highestFloat == FloatPriority . None ) { return CastTo ( expressions , intType ) ; } return CastTo ( expressions , floatType ) ; }
Add an Expression.Convert to the numeric expressions requiring the cast
Add an Expression . Convert to the numeric expressions requiring the cast
// no need to cast // if there are decimals and floats or doubles ==> double
A new list of the same expressions where each one has been casted, if needed, to the correct type
A new list of the same expressions where each one has been casted if needed to the correct type
highestIntType
Type
Type highest float int to impose as a minimum required cast
Type highest float int to impose as a minimum required cast
highestFloatType
Type
Type highest float type to impose as a minimum required cast
Type highest float type to impose as a minimum required cast
highestDecimalType
Type
Type highest decimal type to impose as a minimum required cast
Type highest decimal type to impose as a minimum required cast
expressions
The list of expressions to evaluate
The list of expressions to evaluate