private static object EvaluateBinaryOperation(CodeBinaryOperatorExpression binaryExpr, Type lhsType, object lhsValue, CodeBinaryOperatorType operation, Type rhsType, object rhsValue)
        {
            Literal                 literal;
            Literal                 literal2;
            ArithmeticLiteral       literal3;
            ArithmeticLiteral       literal4;
            RuleEvaluationException exception;

            switch (operation)
            {
            case CodeBinaryOperatorType.Add:
                literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                if (literal3 == null)
                {
                    break;
                }
                literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                if (literal4 == null)
                {
                    break;
                }
                return(literal3.Add(literal4));

            case CodeBinaryOperatorType.Subtract:
                literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                if (literal3 == null)
                {
                    break;
                }
                literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                if (literal4 == null)
                {
                    break;
                }
                return(literal3.Subtract(literal4));

            case CodeBinaryOperatorType.Multiply:
                literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                if (literal3 == null)
                {
                    break;
                }
                literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                if (literal4 == null)
                {
                    break;
                }
                return(literal3.Multiply(literal4));

            case CodeBinaryOperatorType.Divide:
                literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                if (literal3 == null)
                {
                    break;
                }
                literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                if (literal4 == null)
                {
                    break;
                }
                return(literal3.Divide(literal4));

            case CodeBinaryOperatorType.Modulus:
                literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                if (literal3 == null)
                {
                    break;
                }
                literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                if (literal4 == null)
                {
                    break;
                }
                return(literal3.Modulus(literal4));

            case CodeBinaryOperatorType.IdentityInequality:
                return(lhsValue != rhsValue);

            case CodeBinaryOperatorType.IdentityEquality:
                return(lhsValue == rhsValue);

            case CodeBinaryOperatorType.ValueEquality:
                literal = Literal.MakeLiteral(lhsType, lhsValue);
                if (literal == null)
                {
                    break;
                }
                literal2 = Literal.MakeLiteral(rhsType, rhsValue);
                if (literal2 == null)
                {
                    break;
                }
                return(literal.Equal(literal2));

            case CodeBinaryOperatorType.BitwiseOr:
                literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                if (literal3 == null)
                {
                    break;
                }
                literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                if (literal4 == null)
                {
                    break;
                }
                return(literal3.BitOr(literal4));

            case CodeBinaryOperatorType.BitwiseAnd:
                literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                if (literal3 == null)
                {
                    break;
                }
                literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                if (literal4 == null)
                {
                    break;
                }
                return(literal3.BitAnd(literal4));

            case CodeBinaryOperatorType.LessThan:
                literal = Literal.MakeLiteral(lhsType, lhsValue);
                if (literal == null)
                {
                    break;
                }
                literal2 = Literal.MakeLiteral(rhsType, rhsValue);
                if (literal2 == null)
                {
                    break;
                }
                return(literal.LessThan(literal2));

            case CodeBinaryOperatorType.LessThanOrEqual:
                literal = Literal.MakeLiteral(lhsType, lhsValue);
                if (literal == null)
                {
                    break;
                }
                literal2 = Literal.MakeLiteral(rhsType, rhsValue);
                if (literal2 == null)
                {
                    break;
                }
                return(literal.LessThanOrEqual(literal2));

            case CodeBinaryOperatorType.GreaterThan:
                literal = Literal.MakeLiteral(lhsType, lhsValue);
                if (literal == null)
                {
                    break;
                }
                literal2 = Literal.MakeLiteral(rhsType, rhsValue);
                if (literal2 == null)
                {
                    break;
                }
                return(literal.GreaterThan(literal2));

            case CodeBinaryOperatorType.GreaterThanOrEqual:
                literal = Literal.MakeLiteral(lhsType, lhsValue);
                if (literal == null)
                {
                    break;
                }
                literal2 = Literal.MakeLiteral(rhsType, rhsValue);
                if (literal2 == null)
                {
                    break;
                }
                return(literal.GreaterThanOrEqual(literal2));

            default:
                exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpNotSupported, new object[] { operation.ToString() }));
                exception.Data["ErrorObject"] = binaryExpr;
                throw exception;
            }
            exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpFails, new object[] { operation.ToString(), RuleDecompiler.DecompileType(lhsType), RuleDecompiler.DecompileType(rhsType) }));
            exception.Data["ErrorObject"] = binaryExpr;
            throw exception;
        }
Пример #2
0
        private static bool AdjustValueStandard(Type operandType, object operandValue, Type toType, out object converted)
        {
            converted = operandValue;
            if (operandValue == null)
            {
                ValidationError error;
                if (!toType.IsValueType)
                {
                    return(true);
                }
                if (!ConditionHelper.IsNullableValueType(toType))
                {
                    throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, Messages.CannotCastNullToValueType, new object[] { RuleDecompiler.DecompileType(toType) }));
                }
                converted = Activator.CreateInstance(toType);
                return(RuleValidation.StandardImplicitConversion(operandType, toType, null, out error));
            }
            Type c = operandValue.GetType();

            if (c == toType)
            {
                return(true);
            }
            if (toType.IsAssignableFrom(c))
            {
                return(true);
            }
            if (c.IsValueType && toType.IsValueType)
            {
                if (c.IsEnum)
                {
                    c            = Enum.GetUnderlyingType(c);
                    operandValue = ArithmeticLiteral.MakeLiteral(c, operandValue).Value;
                }
                bool flag     = ConditionHelper.IsNullableValueType(toType);
                Type enumType = flag ? Nullable.GetUnderlyingType(toType) : toType;
                if (enumType.IsEnum)
                {
                    object obj2;
                    Type   underlyingType = Enum.GetUnderlyingType(enumType);
                    if (AdjustValueStandard(c, operandValue, underlyingType, out obj2))
                    {
                        converted = Enum.ToObject(enumType, obj2);
                        if (flag)
                        {
                            converted = Activator.CreateInstance(toType, new object[] { converted });
                        }
                        return(true);
                    }
                }
                else if (enumType.IsPrimitive || (enumType == typeof(decimal)))
                {
                    if (c == typeof(char))
                    {
                        char ch = (char)operandValue;
                        if (enumType == typeof(float))
                        {
                            converted = (float)ch;
                        }
                        else if (enumType == typeof(double))
                        {
                            converted = (double)ch;
                        }
                        else if (enumType == typeof(decimal))
                        {
                            converted = ch;
                        }
                        else
                        {
                            converted = ((IConvertible)ch).ToType(enumType, CultureInfo.CurrentCulture);
                        }
                        if (flag)
                        {
                            converted = Activator.CreateInstance(toType, new object[] { converted });
                        }
                        return(true);
                    }
                    if (c == typeof(float))
                    {
                        float num = (float)operandValue;
                        if (enumType == typeof(char))
                        {
                            converted = (char)((ushort)num);
                        }
                        else
                        {
                            converted = ((IConvertible)num).ToType(enumType, CultureInfo.CurrentCulture);
                        }
                        if (flag)
                        {
                            converted = Activator.CreateInstance(toType, new object[] { converted });
                        }
                        return(true);
                    }
                    if (c == typeof(double))
                    {
                        double num2 = (double)operandValue;
                        if (enumType == typeof(char))
                        {
                            converted = (char)((ushort)num2);
                        }
                        else
                        {
                            converted = ((IConvertible)num2).ToType(enumType, CultureInfo.CurrentCulture);
                        }
                        if (flag)
                        {
                            converted = Activator.CreateInstance(toType, new object[] { converted });
                        }
                        return(true);
                    }
                    if (c == typeof(decimal))
                    {
                        decimal num3 = (decimal)operandValue;
                        if (enumType == typeof(char))
                        {
                            converted = (char)num3;
                        }
                        else
                        {
                            converted = ((IConvertible)num3).ToType(enumType, CultureInfo.CurrentCulture);
                        }
                        if (flag)
                        {
                            converted = Activator.CreateInstance(toType, new object[] { converted });
                        }
                        return(true);
                    }
                    IConvertible convertible = operandValue as IConvertible;
                    if (convertible != null)
                    {
                        try
                        {
                            converted = convertible.ToType(enumType, CultureInfo.CurrentCulture);
                            if (flag)
                            {
                                converted = Activator.CreateInstance(toType, new object[] { converted });
                            }
                            return(true);
                        }
                        catch (InvalidCastException)
                        {
                            return(false);
                        }
                    }
                }
            }
            return(false);
        }
Пример #3
0
        private static bool AdjustValueStandard(Type operandType, object operandValue, Type toType, out object converted)
        {
            // assume it's the same for now
            converted = operandValue;

            // check for null
            if (operandValue == null)
            {
                // are we converting to a value type?
                if (toType.IsValueType)
                {
                    // is the conversion to nullable?
                    if (!ConditionHelper.IsNullableValueType(toType))
                    {
                        // value type and null, so no conversion possible
                        string message = string.Format(CultureInfo.CurrentCulture, Messages.CannotCastNullToValueType, RuleDecompiler.DecompileType(toType));
                        throw new InvalidCastException(message);
                    }

                    // here we have a Nullable<T>
                    // however, we may need to call the implicit conversion operator if the types are not compatible
                    converted = Activator.CreateInstance(toType);
                    return(RuleValidation.StandardImplicitConversion(operandType, toType, null, out ValidationError error));
                }

                // not a value type, so null is valid
                return(true);
            }

            // check simple cases
            Type currentType = operandValue.GetType();

            if (currentType == toType)
            {
                return(true);
            }

            // now the fun begins
            // this should handle most class conversions
            if (toType.IsAssignableFrom(currentType))
            {
                return(true);
            }

            // handle the numerics (both implicit and explicit), along with nullable
            // note that if the value was null, it's already handled, so value cannot be nullable
            if ((currentType.IsValueType) && (toType.IsValueType))
            {
                if (currentType.IsEnum)
                {
                    // strip off the enum representation
                    currentType = Enum.GetUnderlyingType(currentType);
                    ArithmeticLiteral literal = ArithmeticLiteral.MakeLiteral(currentType, operandValue);
                    operandValue = literal.Value;
                }

                bool resultNullable = ConditionHelper.IsNullableValueType(toType);
                Type resultType     = (resultNullable) ? Nullable.GetUnderlyingType(toType) : toType;

                if (resultType.IsEnum)
                {
                    // Enum.ToObject may throw if currentType is not type SByte,
                    // Int16, Int32, Int64, Byte, UInt16, UInt32, or UInt64.
                    // So we adjust currentValue to the underlying type (which may throw if out of range)
                    Type underlyingType = Enum.GetUnderlyingType(resultType);
                    if (AdjustValueStandard(currentType, operandValue, underlyingType, out object adjusted))
                    {
                        converted = Enum.ToObject(resultType, adjusted);
                        if (resultNullable)
                        {
                            converted = Activator.CreateInstance(toType, converted);
                        }
                        return(true);
                    }
                }
                else if ((resultType.IsPrimitive) || (resultType == typeof(decimal)))
                {
                    // resultType must be a primitive to continue (not a struct)
                    // (enums and generics handled above)
                    if (currentType == typeof(char))
                    {
                        char c = (char)operandValue;
                        if (resultType == typeof(float))
                        {
                            converted = (float)c;
                        }
                        else if (resultType == typeof(double))
                        {
                            converted = (double)c;
                        }
                        else if (resultType == typeof(decimal))
                        {
                            converted = (decimal)c;
                        }
                        else
                        {
                            converted = ((IConvertible)c).ToType(resultType, CultureInfo.CurrentCulture);
                        }
                        if (resultNullable)
                        {
                            converted = Activator.CreateInstance(toType, converted);
                        }
                        return(true);
                    }
                    else if (currentType == typeof(float))
                    {
                        float f = (float)operandValue;
                        if (resultType == typeof(char))
                        {
                            converted = (char)f;
                        }
                        else
                        {
                            converted = ((IConvertible)f).ToType(resultType, CultureInfo.CurrentCulture);
                        }
                        if (resultNullable)
                        {
                            converted = Activator.CreateInstance(toType, converted);
                        }
                        return(true);
                    }
                    else if (currentType == typeof(double))
                    {
                        double d = (double)operandValue;
                        if (resultType == typeof(char))
                        {
                            converted = (char)d;
                        }
                        else
                        {
                            converted = ((IConvertible)d).ToType(resultType, CultureInfo.CurrentCulture);
                        }
                        if (resultNullable)
                        {
                            converted = Activator.CreateInstance(toType, converted);
                        }
                        return(true);
                    }
                    else if (currentType == typeof(decimal))
                    {
                        decimal d = (decimal)operandValue;
                        if (resultType == typeof(char))
                        {
                            converted = (char)d;
                        }
                        else
                        {
                            converted = ((IConvertible)d).ToType(resultType, CultureInfo.CurrentCulture);
                        }
                        if (resultNullable)
                        {
                            converted = Activator.CreateInstance(toType, converted);
                        }
                        return(true);
                    }
                    else
                    {
                        if (operandValue is IConvertible convert)
                        {
                            try
                            {
                                converted = convert.ToType(resultType, CultureInfo.CurrentCulture);
                                if (resultNullable)
                                {
                                    converted = Activator.CreateInstance(toType, converted);
                                }
                                return(true);
                            }
                            catch (InvalidCastException)
                            {
                                // not IConvertable, so can't do it
                                return(false);
                            }
                        }
                    }
                }
            }

            // no luck with standard conversions, so no conversion done
            return(false);
        }
        public override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
        {
            object            obj2;
            ArithmeticLiteral literal;
            ArithmeticLiteral literal2;
            Literal           literal3;
            Literal           literal4;

            if (this.lhsRootType == null)
            {
                this.lhsRootType = Enum.GetUnderlyingType(this.lhsBaseType);
            }
            if (this.rhsRootType == null)
            {
                this.rhsRootType = Enum.GetUnderlyingType(this.rhsBaseType);
            }
            switch (this.op)
            {
            case CodeBinaryOperatorType.Add:
                if ((parameters[0] != null) && (parameters[1] != null))
                {
                    literal  = ArithmeticLiteral.MakeLiteral(this.lhsRootType, parameters[0]);
                    literal2 = ArithmeticLiteral.MakeLiteral(this.rhsRootType, parameters[1]);
                    obj2     = literal.Add(literal2);
                    obj2     = Executor.AdjustType(obj2.GetType(), obj2, this.resultBaseType);
                    if (this.resultIsNullable)
                    {
                        obj2 = Activator.CreateInstance(this.resultType, new object[] { obj2 });
                    }
                    return(obj2);
                }
                return(null);

            case CodeBinaryOperatorType.Subtract:
                if ((parameters[0] != null) && (parameters[1] != null))
                {
                    literal  = ArithmeticLiteral.MakeLiteral(this.resultRootType, Executor.AdjustType(this.lhsRootType, parameters[0], this.resultRootType));
                    literal2 = ArithmeticLiteral.MakeLiteral(this.resultRootType, Executor.AdjustType(this.rhsRootType, parameters[1], this.resultRootType));
                    obj2     = literal.Subtract(literal2);
                    obj2     = Executor.AdjustType(obj2.GetType(), obj2, this.resultBaseType);
                    if (this.resultIsNullable)
                    {
                        obj2 = Activator.CreateInstance(this.resultType, new object[] { obj2 });
                    }
                    return(obj2);
                }
                return(null);

            case CodeBinaryOperatorType.ValueEquality:
                literal3 = Literal.MakeLiteral(this.lhsRootType, parameters[0]);
                literal4 = Literal.MakeLiteral(this.rhsRootType, parameters[1]);
                return(literal3.Equal(literal4));

            case CodeBinaryOperatorType.LessThan:
                literal3 = Literal.MakeLiteral(this.lhsRootType, parameters[0]);
                literal4 = Literal.MakeLiteral(this.rhsRootType, parameters[1]);
                return(literal3.LessThan(literal4));

            case CodeBinaryOperatorType.LessThanOrEqual:
                literal3 = Literal.MakeLiteral(this.lhsRootType, parameters[0]);
                literal4 = Literal.MakeLiteral(this.rhsRootType, parameters[1]);
                return(literal3.LessThanOrEqual(literal4));

            case CodeBinaryOperatorType.GreaterThan:
                literal3 = Literal.MakeLiteral(this.lhsRootType, parameters[0]);
                literal4 = Literal.MakeLiteral(this.rhsRootType, parameters[1]);
                return(literal3.GreaterThan(literal4));

            case CodeBinaryOperatorType.GreaterThanOrEqual:
                literal3 = Literal.MakeLiteral(this.lhsRootType, parameters[0]);
                literal4 = Literal.MakeLiteral(this.rhsRootType, parameters[1]);
                return(literal3.GreaterThanOrEqual(literal4));
            }
            throw new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpNotSupported, new object[] { this.op.ToString() }));
        }