コード例 #1
0
ファイル: ExprMathNode.cs プロジェクト: qinfengzhu/nesper
        public override ExprNode Validate(ExprValidationContext validationContext)
        {
            if (ChildNodes.Count != 2)
            {
                throw new ExprValidationException("Arithmatic node must have 2 parameters");
            }

            foreach (ExprNode child in ChildNodes)
            {
                var childType = child.ExprEvaluator.ReturnType;
                if (!childType.IsNumeric())
                {
                    throw new ExprValidationException(
                              string.Format("Implicit conversion from datatype '{0}' to numeric is not allowed", Name.Clean(childType)));
                }
            }

            // Determine result type, set up compute function
            _evaluatorLeft  = ChildNodes[0].ExprEvaluator;
            _evaluatorRight = ChildNodes[1].ExprEvaluator;

            var childTypeOne = _evaluatorLeft.ReturnType;
            var childTypeTwo = _evaluatorRight.ReturnType;

            if ((childTypeOne == typeof(short) || childTypeOne == typeof(short?)) &&
                (childTypeTwo == typeof(short) || childTypeTwo == typeof(short?)))
            {
                _resultType = typeof(int?);
            }
            else if ((childTypeOne == typeof(byte) || childTypeOne == typeof(byte?)) &&
                     (childTypeTwo == typeof(byte) || childTypeTwo == typeof(byte?)))
            {
                _resultType = typeof(int?);
            }
            else if (childTypeOne == childTypeTwo)
            {
                _resultType = childTypeTwo.GetBoxedType();
            }
            else
            {
                _resultType = childTypeOne.GetArithmaticCoercionType(childTypeTwo);
            }

            if ((_mathArithTypeEnum == MathArithTypeEnum.DIVIDE) && (!_isIntegerDivision))
            {
                if (_resultType != typeof(decimal?))
                {
                    _resultType = typeof(double?);
                }
            }

            _arithTypeEnumComputer = _mathArithTypeEnum.GetComputer(_resultType, childTypeOne, childTypeTwo, _isIntegerDivision, _isDivisionByZeroReturnsNull, validationContext.EngineImportService.DefaultMathContext);

            return(null);
        }
コード例 #2
0
ファイル: TestArithTypeEnum.cs プロジェクト: valmac/nesper
 public void TestAddDouble()
 {
     MathArithTypeEnumExtensions.Computer computer = MathArithTypeEnum.ADD.GetComputer(typeof(double?), typeof(double?), typeof(double?), false, false, null);
     Assert.AreEqual(12.1d, computer.Invoke(5.5, 6.6));
 }