public override void ValidateActualValue(object actual) { Guard.ArgumentNotNull(actual, nameof(actual)); if (!(actual is TExpected)) { Guard.ArgumentValid( Numerics.IsNumericType(actual) && Numerics.IsNumericType(typeof(TExpected)), "Range comparisons are only supported for numeric types.", nameof(actual)); } }
public override void ValidateActualValue(object actual) { Guard.ArgumentNotNull(actual, nameof(actual)); if (!(actual is TExpected)) { Guard.ArgumentValid( Numerics.IsNumericType(actual) && Numerics.IsNumericType(typeof(TExpected)), "Comparisons between objects of different types require both to be numeric types.", nameof(actual)); } }
/// <summary> /// Tests that the current Tolerance is linear with a /// numeric value, throwing an exception if it is not. /// </summary> private void CheckLinearAndNumeric() { if (Mode != ToleranceMode.Linear) { throw new InvalidOperationException(Mode == ToleranceMode.Default ? ModeMustFollowTolerance : MultipleToleranceModes); } if (!Numerics.IsNumericType(Amount)) { throw new InvalidOperationException(NumericToleranceRequired); } }
/// <summary> /// Compares two objects /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public int Compare(object x, object y) { if (x == null) { return(y == null ? 0 : -1); } else if (y == null) { return(+1); } if (Numerics.IsNumericType(x) && Numerics.IsNumericType(y)) { return(Numerics.Compare(x, y)); } if (x is IComparable) { return(((IComparable)x).CompareTo(y)); } if (y is IComparable) { return(-((IComparable)y).CompareTo(x)); } Type xType = x.GetType(); Type yType = y.GetType(); MethodInfo method = xType.GetMethod("CompareTo", new Type[] { yType }); if (method != null) { return((int)method.Invoke(x, new object[] { y })); } method = yType.GetMethod("CompareTo", new Type[] { xType }); if (method != null) { return(-(int)method.Invoke(y, new object[] { x })); } throw new ArgumentException("Neither value implements IComparable or IComparable<T>"); }