internal static object DoConversionFrom(TypeConverter converter, object value) { object returnValue = value; try { if (converter != null && value != null && converter.CanConvertFrom(value.GetType())) { // This utility class is used to convert value that come from XAML, so we should use the invariant culture. returnValue = converter.ConvertFrom(context: null, culture: CultureInfo.InvariantCulture, value: value); } } catch (Exception e) { if (!TypeConverterHelper.ShouldEatException(e)) { throw; } } return(returnValue); }
/// <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(ComparisonLogic.EvaluateComparable(leftComparableOperand, operatorType, rightComparableOperand)); } switch (operatorType) { case ComparisonConditionType.Equal: result = object.Equals(leftOperand, rightOperand); break; case ComparisonConditionType.NotEqual: result = !object.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); }