Contains type-related functionality that isn't conversion or comparison.
コード例 #1
0
ファイル: TypeComparer.cs プロジェクト: xpologistics/jurassic
        /// <summary>
        /// Compares two objects for equality.  Used by the equality operator (==).
        /// </summary>
        /// <param name="x"> The first object to compare. </param>
        /// <param name="y"> The first object to compare. </param>
        /// <returns> <c>true</c> if the objects are identical; <c>false</c> otherwise. </returns>
        public new static bool Equals(object x, object y)
        {
            x = x ?? Undefined.Value;
            y = y ?? Undefined.Value;
            if (x is int)
            {
                x = (double)(int)x;
            }
            if (x is uint)
            {
                x = (double)(uint)x;
            }
            if (y is int)
            {
                y = (double)(int)y;
            }
            if (y is uint)
            {
                y = (double)(uint)y;
            }
            if (x is ConcatenatedString)
            {
                x = x.ToString();
            }
            if (y is ConcatenatedString)
            {
                y = y.ToString();
            }
            if (x.GetType() == y.GetType())
            {
                if (x is double && double.IsNaN((double)x) == true)
                {
                    return(false);
                }
                return(object.Equals(x, y));
            }
            if ((x == Undefined.Value && y == Null.Value) || (x == Null.Value && y == Undefined.Value))
            {
                return(true);
            }

            // 5.5 == "5.5"
            if (TypeUtilities.IsNumeric(x) && TypeUtilities.IsString(y))
            {
                return(TypeConverter.ToNumber(x) == TypeConverter.ToNumber(y));
            }
            if (TypeUtilities.IsString(x) && TypeUtilities.IsNumeric(y))
            {
                return(TypeConverter.ToNumber(x) == TypeConverter.ToNumber(y));
            }

            // false == 0, true == 1
            if (x is bool)
            {
                return(Equals(TypeConverter.ToNumber(x), y));
            }
            if (y is bool)
            {
                return(Equals(x, TypeConverter.ToNumber(y)));
            }

            // false == new Boolean(false), 1.5 == new Number(1.5)
            if (TypeUtilities.IsNumeric(x) && y is Jurassic.Library.ObjectInstance)
            {
                return(Equals(x, TypeConverter.ToPrimitive(y, PrimitiveTypeHint.None)));
            }
            if (TypeUtilities.IsString(x) && y is Jurassic.Library.ObjectInstance)
            {
                return(Equals(x, TypeConverter.ToPrimitive(y, PrimitiveTypeHint.None)));
            }
            if (x is Jurassic.Library.ObjectInstance && TypeUtilities.IsNumeric(y))
            {
                return(Equals(TypeConverter.ToPrimitive(x, PrimitiveTypeHint.None), y));
            }
            if (x is Jurassic.Library.ObjectInstance && TypeUtilities.IsString(y))
            {
                return(Equals(TypeConverter.ToPrimitive(x, PrimitiveTypeHint.None), y));
            }

            return(false);
        }