Esempio n. 1
0
        /// <summary>
        /// Method to compare two objects
        /// </summary>
        /// <param name="leftOperand"></param>
        /// <param name="operatorType"></param>
        /// <param name="rightOperand"></param>
        /// <returns></returns>
        internal static bool Evaluate(object leftOperand, ComparisonConditionType operatorType, object rightOperand)
        {
            if (leftOperand != null)
            {
                if (rightOperand != null)
                {
                    object newValue = PlatformServices.ConvertType(rightOperand, leftOperand.GetType());
                    if (newValue != null)
                    {
                        rightOperand = newValue;
                    }
                }
            }

            IComparable comparable  = leftOperand as IComparable;
            IComparable comparable2 = rightOperand as IComparable;

            if ((comparable != null) && (comparable2 != null))
            {
                return(EvaluateComparable(comparable, operatorType, comparable2));
            }

            switch (operatorType)
            {
            case ComparisonConditionType.Equal:
                return(Equals(leftOperand, rightOperand));

            case ComparisonConditionType.NotEqual:
                return(!Equals(leftOperand, rightOperand));

            case ComparisonConditionType.LessThan:
            case ComparisonConditionType.LessThanOrEqual:
            case ComparisonConditionType.GreaterThan:
            case ComparisonConditionType.GreaterThanOrEqual:
                if ((comparable == null) && (comparable2 == null))
                {
                    throw new ArgumentException("Operands must implement IComparable");
                }
                if (comparable == null)
                {
                    throw new ArgumentException("Invalid left operand - must implement IComparable");
                }
                throw new ArgumentException("Invalid right operand - must implement IComparable.");
            }

            return(false);
        }