private static bool Compare(object leftOperand, ComparisonConditionType operatorType, object rightOperand) { if (leftOperand != null && rightOperand != null) { rightOperand = TypeConverterHelper.Convert(rightOperand.ToString(), leftOperand.GetType().FullName); } IComparable leftComparableOperand = leftOperand as IComparable; IComparable rightComparableOperand = rightOperand as IComparable; if ((leftComparableOperand != null) && (rightComparableOperand != null)) { return(DataTriggerBehavior.EvaluateComparable(leftComparableOperand, operatorType, rightComparableOperand)); } switch (operatorType) { case ComparisonConditionType.Equal: return(object.Equals(leftOperand, rightOperand)); case ComparisonConditionType.NotEqual: return(!object.Equals(leftOperand, rightOperand)); case ComparisonConditionType.LessThan: case ComparisonConditionType.LessThanOrEqual: case ComparisonConditionType.GreaterThan: case ComparisonConditionType.GreaterThanOrEqual: { if (leftComparableOperand == null && rightComparableOperand == null) { throw new ArgumentException(string.Format( CultureInfo.CurrentCulture, ResourceHelper.InvalidOperands, leftOperand != null ? leftOperand.GetType().Name : "null", rightOperand != null ? rightOperand.GetType().Name : "null", operatorType.ToString())); } else if (leftComparableOperand == null) { throw new ArgumentException(string.Format( CultureInfo.CurrentCulture, ResourceHelper.InvalidLeftOperand, leftOperand != null ? leftOperand.GetType().Name : "null", operatorType.ToString())); } else { throw new ArgumentException(string.Format( CultureInfo.CurrentCulture, ResourceHelper.InvalidRightOperand, rightOperand != null ? rightOperand.GetType().Name : "null", operatorType.ToString())); } } } return(false); }
private static bool Compare(object leftOperand, ComparisonConditionType operatorType, object rightOperand) { if (leftOperand != null && rightOperand != null) { rightOperand = TypeConverterHelper.Convert(rightOperand.ToString(), leftOperand.GetType().FullName); } IComparable comparable = leftOperand as IComparable; IComparable comparable2 = rightOperand as IComparable; if (comparable != null && comparable2 != null) { return(DataTriggerBehavior.EvaluateComparable(comparable, operatorType, comparable2)); } switch (operatorType) { case ComparisonConditionType.Equal: return(object.Equals(leftOperand, rightOperand)); case ComparisonConditionType.NotEqual: return(!object.Equals(leftOperand, rightOperand)); case ComparisonConditionType.LessThan: case ComparisonConditionType.LessThanOrEqual: case ComparisonConditionType.GreaterThan: case ComparisonConditionType.GreaterThanOrEqual: if (comparable == null && comparable2 == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "InvalidOperands", new object[] { (leftOperand != null) ? leftOperand.GetType().Name : "null", (rightOperand != null) ? rightOperand.GetType().Name : "null", operatorType.ToString() })); } if (comparable == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "InvalidLeftOperand", new object[] { (leftOperand != null) ? leftOperand.GetType().Name : "null", operatorType.ToString() })); } throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "InvalidRightOperand", new object[] { (rightOperand != null) ? rightOperand.GetType().Name : "null", operatorType.ToString() })); default: return(false); } }
/// <summary> /// This method evaluates operands. /// </summary> /// <param name="leftOperand">Left operand from the LeftOperand property.</param> /// <param name="operatorType">Operator from Operator property.</param> /// <param name="rightOperand">Right operand from the RightOperand property.</param> /// <returns>Returns true if the condition is met; otherwise, returns false.</returns> internal static bool EvaluateImpl(object leftOperand, ComparisonConditionType operatorType, object rightOperand) { bool result = false; if (leftOperand != null) { Type leftType = leftOperand.GetType(); if (rightOperand != null) { TypeConverter typeConverter = TypeConverterHelper.GetTypeConverter(leftType); rightOperand = TypeConverterHelper.DoConversionFrom(typeConverter, rightOperand); } } IComparable leftComparableOperand = leftOperand as IComparable; IComparable rightComparableOperand = rightOperand as IComparable; // If both operands are comparable, use arithmetic comparison if (leftComparableOperand != null && rightComparableOperand != null) { return(EvaluateComparable(leftComparableOperand, operatorType, rightComparableOperand)); } switch (operatorType) { case ComparisonConditionType.Equal: result = Equals(leftOperand, rightOperand); break; case ComparisonConditionType.NotEqual: result = !Equals(leftOperand, rightOperand); break; case ComparisonConditionType.GreaterThan: case ComparisonConditionType.GreaterThanOrEqual: case ComparisonConditionType.LessThan: case ComparisonConditionType.LessThanOrEqual: if (leftComparableOperand == null && rightComparableOperand == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, ExceptionStringTable.InvalidOperands, leftOperand != null ? leftOperand.GetType().Name : "null", rightOperand != null ? rightOperand.GetType().Name : "null", operatorType.ToString())); } else if (leftComparableOperand == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, ExceptionStringTable.InvalidLeftOperand, leftOperand != null ? leftOperand.GetType().Name : "null", operatorType.ToString())); } else { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, ExceptionStringTable.InvalidRightOperand, rightOperand != null ? rightOperand.GetType().Name : "null", operatorType.ToString())); } } return(result); }
private static bool Compare(object leftOperand, ComparisonConditionType operatorType, object rightOperand) { if (leftOperand != null && rightOperand != null) { rightOperand = TypeConverterHelper.Convert(rightOperand.ToString(), leftOperand.GetType().FullName); } IComparable leftComparableOperand = leftOperand as IComparable; IComparable rightComparableOperand = rightOperand as IComparable; if ((leftComparableOperand != null) && (rightComparableOperand != null)) { return DataTriggerBehavior.EvaluateComparable(leftComparableOperand, operatorType, rightComparableOperand); } switch (operatorType) { case ComparisonConditionType.Equal: return object.Equals(leftOperand, rightOperand); case ComparisonConditionType.NotEqual: return !object.Equals(leftOperand, rightOperand); case ComparisonConditionType.LessThan: case ComparisonConditionType.LessThanOrEqual: case ComparisonConditionType.GreaterThan: case ComparisonConditionType.GreaterThanOrEqual: { if (leftComparableOperand == null && rightComparableOperand == null) { throw new ArgumentException(string.Format( CultureInfo.CurrentCulture, ResourceHelper.InvalidOperands, leftOperand != null ? leftOperand.GetType().Name : "null", rightOperand != null ? rightOperand.GetType().Name : "null", operatorType.ToString())); } else if (leftComparableOperand == null) { throw new ArgumentException(string.Format( CultureInfo.CurrentCulture, ResourceHelper.InvalidLeftOperand, leftOperand != null ? leftOperand.GetType().Name : "null", operatorType.ToString())); } else { throw new ArgumentException(string.Format( CultureInfo.CurrentCulture, ResourceHelper.InvalidRightOperand, rightOperand != null ? rightOperand.GetType().Name : "null", operatorType.ToString())); } } } return false; }
/// <summary> /// This method evaluates operands. /// </summary> /// <param name="leftOperand">Left operand from the LeftOperand property.</param> /// <param name="operatorType">Operator from Operator property.</param> /// <param name="rightOperand">Right operand from the RightOperand property.</param> /// <returns>Returns true if the condition is met; otherwise, returns false.</returns> internal static bool EvaluateImpl(object leftOperand, ComparisonConditionType operatorType, object rightOperand) { bool result = false; if (leftOperand != null) { Type type = leftOperand.GetType(); if (rightOperand != null) { var typeConverter = TypeConverterHelper.GetTypeConverter(type); rightOperand = TypeConverterHelper.DoConversionFrom(typeConverter, rightOperand); } } IComparable leftOperandComparable = leftOperand as IComparable; IComparable rightOperandComparable = rightOperand as IComparable; if (leftOperandComparable != null && rightOperandComparable != null) { return(EvaluateComparable(leftOperandComparable, operatorType, rightOperandComparable)); } switch (operatorType) { case ComparisonConditionType.Equal: result = object.Equals(leftOperand, rightOperand); break; case ComparisonConditionType.NotEqual: result = !object.Equals(leftOperand, rightOperand); break; case ComparisonConditionType.LessThan: case ComparisonConditionType.LessThanOrEqual: case ComparisonConditionType.GreaterThan: case ComparisonConditionType.GreaterThanOrEqual: if (leftOperandComparable == null && rightOperandComparable == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "LeftOperand of type '{1}' and RightOperand of type '{0}' cannot be used with operator '{2}'.", (leftOperand != null) ? leftOperand.GetType().Name : "null", (rightOperand != null) ? rightOperand.GetType().Name : "null", operatorType.ToString())); } if (leftOperandComparable == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "LeftOperand of type '{0}' cannot be used with operator '{1}'.", (leftOperand != null) ? leftOperand.GetType().Name : "null", operatorType.ToString())); } throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "RightOperand of type '{0}' cannot be used with operator '{1}'.", (rightOperand != null) ? rightOperand.GetType().Name : "null", operatorType.ToString())); } return(result); }
/// <summary> /// This method evaluates operands. /// </summary> /// <param name="leftOperand">Left operand from the LeftOperand property.</param> /// <param name="operatorType">Operator from Operator property.</param> /// <param name="rightOperand">Right operand from the RightOperand property.</param> /// <returns>Returns true if the condition is met; otherwise, returns false.</returns> internal static bool EvaluateImpl(object leftOperand, ComparisonConditionType operatorType, object rightOperand) { if (leftOperand != null) { Type type = leftOperand.GetType(); if (rightOperand != null) { //rightOperand = TypeConverterHelper.DoConversionFrom(TypeConverterHelper.GetTypeConverter(type), rightOperand); if (type.GetTypeInfo().IsEnum && Enum.IsDefined(type, rightOperand)) rightOperand = Enum.ToObject(type, rightOperand); } } 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 object.Equals(leftOperand, rightOperand); case ComparisonConditionType.NotEqual: return !object.Equals(leftOperand, rightOperand); case ComparisonConditionType.LessThan: case ComparisonConditionType.LessThanOrEqual: case ComparisonConditionType.GreaterThan: case ComparisonConditionType.GreaterThanOrEqual: if ((comparable == null) && (comparable2 == null)) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, ExceptionStringTableHelper.InvalidOperands, new object[] { (leftOperand != null) ? leftOperand.GetType().Name : "null", (rightOperand != null) ? rightOperand.GetType().Name : "null", operatorType.ToString() })); } if (comparable == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, ExceptionStringTableHelper.InvalidLeftOperand, new object[] { (leftOperand != null) ? leftOperand.GetType().Name : "null", operatorType.ToString() })); } throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, ExceptionStringTableHelper.InvalidRightOperand, new object[] { (rightOperand != null) ? rightOperand.GetType().Name : "null", operatorType.ToString() })); } return false; }
private static bool Compare(object leftOperand, ComparisonConditionType operatorType, object rightOperand) { if (leftOperand != null && rightOperand != null) { rightOperand = TypeConverterHelper.Convert(rightOperand.ToString(), leftOperand.GetType().FullName); } IComparable comparable = leftOperand as IComparable; IComparable comparable2 = rightOperand as IComparable; if (comparable != null && comparable2 != null) { return DataTriggerBehavior.EvaluateComparable(comparable, operatorType, comparable2); } switch (operatorType) { case ComparisonConditionType.Equal: return object.Equals(leftOperand, rightOperand); case ComparisonConditionType.NotEqual: return !object.Equals(leftOperand, rightOperand); case ComparisonConditionType.LessThan: case ComparisonConditionType.LessThanOrEqual: case ComparisonConditionType.GreaterThan: case ComparisonConditionType.GreaterThanOrEqual: if (comparable == null && comparable2 == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "InvalidOperands", new object[] { (leftOperand != null) ? leftOperand.GetType().Name : "null", (rightOperand != null) ? rightOperand.GetType().Name : "null", operatorType.ToString() })); } if (comparable == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "InvalidLeftOperand", new object[] { (leftOperand != null) ? leftOperand.GetType().Name : "null", operatorType.ToString() })); } throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "InvalidRightOperand", new object[] { (rightOperand != null) ? rightOperand.GetType().Name : "null", operatorType.ToString() })); default: return false; } }