Exemple #1
0
        /// <summary>
        /// Determines if the two items are equal
        /// </summary>
        /// <param name="x">Object 1</param>
        /// <param name="y">Object 2</param>
        /// <returns>True if they are, false otherwise</returns>
        public bool Equals(TData x, TData y)
        {
            var TypeInfo = typeof(TData);

            if (!TypeInfo.IsValueType ||
                (TypeInfo.IsGenericType &&
                 TypeInfo.GetGenericTypeDefinition().IsAssignableFrom(typeof(Nullable <>))))
            {
                if (object.Equals(x, default(TData) !))
                {
                    return(object.Equals(y, default(TData) !));
                }

                if (object.Equals(y, default(TData) !))
                {
                    return(false);
                }
            }
            if (x?.GetType() != y?.GetType())
            {
                return(false);
            }

            if (x is IEnumerable IEnumerablex && y is IEnumerable IEnumerabley)
            {
                var Comparer    = GenericEqualityComparer <object> .Comparer;
                var XEnumerator = IEnumerablex.GetEnumerator();
                var YEnumerator = IEnumerabley.GetEnumerator();
                while (true)
                {
                    var XFinished = !XEnumerator.MoveNext();
                    var YFinished = !YEnumerator.MoveNext();
                    if (XFinished || YFinished)
                    {
                        return(XFinished && YFinished);
                    }

                    if (!Comparer.Equals(XEnumerator.Current, YEnumerator.Current))
                    {
                        return(false);
                    }
                }
            }
            if (x is IEqualityComparer <TData> TempEquality)
            {
                return(TempEquality.Equals(y));
            }

            if (x is IComparable <TData> TempComparable)
            {
                return(TempComparable.CompareTo(y) == 0);
            }

            if (x is IComparable TempComparable2)
            {
                return(TempComparable2.CompareTo(y) == 0);
            }

            return(x?.Equals(y) ?? false);
        }
        /// <summary>
        /// Compares the two objects
        /// </summary>
        /// <param name="x">Object 1</param>
        /// <param name="y">Object 2</param>
        /// <returns>0 if they're equal, any other value they are not</returns>
        public int Compare(TData x, TData y)
        {
            var TypeInfo = typeof(TData);

            if (!TypeInfo.IsValueType ||
                (TypeInfo.IsGenericType &&
                 TypeInfo.GetGenericTypeDefinition().IsAssignableFrom(typeof(Nullable <>))))
            {
                if (Equals(x, default(TData) !))
                {
                    return(Equals(y, default(TData) !) ? 0 : -1);
                }

                if (Equals(y, default(TData) !))
                {
                    return(-1);
                }
            }
            if (x.GetType() != y.GetType())
            {
                return(-1);
            }

            if (x is IComparable <TData> TempComparable)
            {
                return(TempComparable.CompareTo(y));
            }

            return(x.CompareTo(y));
        }