IsNumericType() public static method

Checks the type of the object, returning true if the object is a numeric type.
public static IsNumericType ( Object obj ) : bool
obj Object The object to check
return bool
コード例 #1
0
ファイル: Tolerance.cs プロジェクト: Acidburn0zzz/PclUnit
        /// <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.None
                    ? ModeMustFollowTolerance
                    : MultipleToleranceModes);
            }

            if (!Numerics.IsNumericType(amount))
            {
                throw new InvalidOperationException(NumericToleranceRequired);
            }
        }
コード例 #2
0
ファイル: NUnitComparer.cs プロジェクト: Acidburn0zzz/PclUnit
        /// <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>");
        }
コード例 #3
0
        /// <summary>
        /// Compares two objects for equality within a tolerance.
        /// </summary>
        public bool AreEqual(object x, object y, ref Tolerance tolerance)
        {
            this.failurePoints = new List <object>();

            if (x == null && y == null)
            {
                return(true);
            }

            if (x == null || y == null)
            {
                return(false);
            }

            if (object.ReferenceEquals(x, y))
            {
                return(true);
            }

            Type xType = x.GetType();
            Type yType = y.GetType();

            EqualityAdapter externalComparer = GetExternalComparer(x, y);

            if (externalComparer != null)
            {
                return(externalComparer.AreEqual(x, y));
            }

            if (xType.IsArray && yType.IsArray && !compareAsCollection)
            {
                return(ArraysEqual((Array)x, (Array)y, ref tolerance));
            }

            if (x is IDictionary && y is IDictionary)
            {
                return(DictionariesEqual((IDictionary)x, (IDictionary)y, ref tolerance));
            }

            //if (x is ICollection && y is ICollection)
            //    return CollectionsEqual((ICollection)x, (ICollection)y, ref tolerance);

            if (x is IEnumerable && y is IEnumerable && !(x is string && y is string))
            {
                return(EnumerablesEqual((IEnumerable)x, (IEnumerable)y, ref tolerance));
            }

            if (x is string && y is string)
            {
                return(StringsEqual((string)x, (string)y));
            }

            if (x is Stream && y is Stream)
            {
                return(StreamsEqual((Stream)x, (Stream)y));
            }

            if (Numerics.IsNumericType(x) && Numerics.IsNumericType(y))
            {
                return(Numerics.AreEqual(x, y, ref tolerance));
            }

            if (tolerance != null && tolerance.Value is TimeSpan)
            {
                TimeSpan amount = (TimeSpan)tolerance.Value;

                if (x is DateTime && y is DateTime)
                {
                    return(((DateTime)x - (DateTime)y).Duration() <= amount);
                }

                if (x is TimeSpan && y is TimeSpan)
                {
                    return(((TimeSpan)x - (TimeSpan)y).Duration() <= amount);
                }
            }


            if (FirstImplementsIEquatableOfSecond(xType, yType))
            {
                return(InvokeFirstIEquatableEqualsSecond(x, y));
            }
            else if (FirstImplementsIEquatableOfSecond(yType, xType))
            {
                return(InvokeFirstIEquatableEqualsSecond(y, x));
            }


            return(x.Equals(y));
        }