Ejemplo n.º 1
0
        static Comparer <T> CreateComparer()
        {
            RuntimeType t = (RuntimeType)typeof(T);

            if (typeof(IComparable <T>).IsAssignableFrom(t))
            {
                return((Comparer <T>)RuntimeType.CreateInstanceForAnotherGenericParameter(typeof(GenericComparer <>), t));
            }

            // If T is a Nullable<U> where U implements IComparable<U> return a NullableComparer<U>
            if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                RuntimeType u = (RuntimeType)t.GetGenericArguments()[0];
                if (typeof(IComparable <>).MakeGenericType(u).IsAssignableFrom(u))
                {
                    return((Comparer <T>)RuntimeType.CreateInstanceForAnotherGenericParameter(typeof(NullableComparer <>), u));
                }
            }

            if (t.IsEnum)
            {
                return((Comparer <T>)RuntimeType.CreateInstanceForAnotherGenericParameter(typeof(EnumComparer <>), t));
            }

            // Otherwise return an ObjectComparer<T>
            return(new ObjectComparer <T> ());
        }
Ejemplo n.º 2
0
        private static EqualityComparer <T> CreateComparer()
        {
            RuntimeType t = (RuntimeType)typeof(T);

            /////////////////////////////////////////////////
            // KEEP THIS IN SYNC WITH THE DEVIRT CODE
            // IN METHOD-TO-IR.C
            /////////////////////////////////////////////////

            if (t == typeof(byte))
            {
                return((EqualityComparer <T>)(object)(new ByteEqualityComparer()));
            }

            if (typeof(IEquatable <T>).IsAssignableFrom(t))
            {
                return((EqualityComparer <T>)RuntimeType.CreateInstanceForAnotherGenericParameter(typeof(GenericEqualityComparer <>), t));
            }

            if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                RuntimeType u = (RuntimeType)t.GetGenericArguments()[0];
                if (typeof(IEquatable <>).MakeGenericType(u).IsAssignableFrom(u))
                {
                    return((EqualityComparer <T>)RuntimeType.CreateInstanceForAnotherGenericParameter(typeof(NullableEqualityComparer <>), u));
                }
            }

            if (t.IsEnum)
            {
                return((EqualityComparer <T>)RuntimeType.CreateInstanceForAnotherGenericParameter(typeof(EnumEqualityComparer <>), t));
            }

            return(new ObjectEqualityComparer <T>());
        }
        [System.Security.SecuritySafeCritical]  // auto-generated
        private static EqualityComparer <T> CreateComparer()
        {
            Contract.Ensures(Contract.Result <EqualityComparer <T> >() != null);

            RuntimeType t = (RuntimeType)typeof(T);

            // Specialize type byte for performance reasons
            if (t == typeof(byte))
            {
                return((EqualityComparer <T>)(object)(new ByteEqualityComparer()));
            }

#if MOBILE
            // Breaks .net serialization compatibility
            if (t == typeof(string))
            {
                return((EqualityComparer <T>)(object) new InternalStringComparer());
            }
#endif

            // If T implements IEquatable<T> return a GenericEqualityComparer<T>
            if (typeof(IEquatable <T>).IsAssignableFrom(t))
            {
#if MONO
                return((EqualityComparer <T>)RuntimeType.CreateInstanceForAnotherGenericParameter(typeof(GenericEqualityComparer <>), t));
#else
                return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericEqualityComparer <int>), t));
#endif
            }
            // If T is a Nullable<U> where U implements IEquatable<U> return a NullableEqualityComparer<U>
            if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                RuntimeType u = (RuntimeType)t.GetGenericArguments()[0];
                if (typeof(IEquatable <>).MakeGenericType(u).IsAssignableFrom(u))
                {
#if MONO
                    return((EqualityComparer <T>)RuntimeType.CreateInstanceForAnotherGenericParameter(typeof(NullableEqualityComparer <>), u));
#else
                    return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableEqualityComparer <int>), u));
#endif
                }
            }
            // If T is an int-based Enum, return an EnumEqualityComparer<T>
            // See the METHOD__JIT_HELPERS__UNSAFE_ENUM_CAST and METHOD__JIT_HELPERS__UNSAFE_ENUM_CAST_LONG cases in getILIntrinsicImplementation
            if (t.IsEnum && Enum.GetUnderlyingType(t) == typeof(int))
            {
#if MONO
                return((EqualityComparer <T>)RuntimeType.CreateInstanceForAnotherGenericParameter(typeof(EnumEqualityComparer <>), t));
#else
                return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(EnumEqualityComparer <int>), t));
#endif
            }
            // Otherwise return an ObjectEqualityComparer<T>
            return(new ObjectEqualityComparer <T>());
        }
Ejemplo n.º 4
0
        private static IArraySortHelper <T> CreateArraySortHelper()
        {
            IArraySortHelper <T> defaultArraySortHelper;

            if (typeof(IComparable <T>).IsAssignableFrom(typeof(T)))
            {
                defaultArraySortHelper = (IArraySortHelper <T>)RuntimeType.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericArraySortHelper <>), (RuntimeType)typeof(T));
            }
            else
            {
                defaultArraySortHelper = new ArraySortHelper <T>();
            }
            return(defaultArraySortHelper);
        }
Ejemplo n.º 5
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        private static Comparer <T> CreateComparer()
        {
            RuntimeType t = (RuntimeType)typeof(T);

            // If T implements IComparable<T> return a GenericComparer<T>
#if FEATURE_LEGACYNETCF
            // Pre-Apollo Windows Phone call the overload that sorts the keys, not values this achieves the same result
            if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
            {
                if (t.ImplementInterface(typeof(IComparable <T>)))
                {
                    return((Comparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericComparer <int>), t));
                }
            }
            else
#endif
            if (typeof(IComparable <T>).IsAssignableFrom(t))
            {
#if MONO
                return((Comparer <T>)RuntimeType.CreateInstanceForAnotherGenericParameter(typeof(GenericComparer <>), t));
#else
                return((Comparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericComparer <int>), t));
#endif
            }

            // If T is a Nullable<U> where U implements IComparable<U> return a NullableComparer<U>
            if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                RuntimeType u = (RuntimeType)t.GetGenericArguments()[0];
                if (typeof(IComparable <>).MakeGenericType(u).IsAssignableFrom(u))
                {
#if MONO
                    return((Comparer <T>)RuntimeType.CreateInstanceForAnotherGenericParameter(typeof(NullableComparer <>), u));
#else
                    return((Comparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableComparer <int>), u));
#endif
                }
            }
            // Otherwise return an ObjectComparer<T>
            return(new ObjectComparer <T>());
        }
        [System.Security.SecuritySafeCritical]  // auto-generated
        private static EqualityComparer <T> CreateComparer()
        {
            Contract.Ensures(Contract.Result <EqualityComparer <T> >() != null);

            RuntimeType t = (RuntimeType)typeof(T);

            // Specialize type byte for performance reasons
            if (t == typeof(byte))
            {
                return((EqualityComparer <T>)(object)(new ByteEqualityComparer()));
            }

            /////////////////////////////////////////////////
            // KEEP THIS IN SYNC WITH THE DEVIRT CODE
            // IN METHOD-TO-IR.C
            /////////////////////////////////////////////////
#if MOBILE
            // Breaks .net serialization compatibility
            if (t == typeof(string))
            {
                return((EqualityComparer <T>)(object) new InternalStringComparer());
            }
#endif

            // If T implements IEquatable<T> return a GenericEqualityComparer<T>
            if (typeof(IEquatable <T>).IsAssignableFrom(t))
            {
#if MONO
                return((EqualityComparer <T>)RuntimeType.CreateInstanceForAnotherGenericParameter(typeof(GenericEqualityComparer <>), t));
#else
                return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericEqualityComparer <int>), t));
#endif
            }
            // If T is a Nullable<U> where U implements IEquatable<U> return a NullableEqualityComparer<U>
            if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                RuntimeType u = (RuntimeType)t.GetGenericArguments()[0];
                if (typeof(IEquatable <>).MakeGenericType(u).IsAssignableFrom(u))
                {
#if MONO
                    return((EqualityComparer <T>)RuntimeType.CreateInstanceForAnotherGenericParameter(typeof(NullableEqualityComparer <>), u));
#else
                    return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableEqualityComparer <int>), u));
#endif
                }
            }

            // See the METHOD__JIT_HELPERS__UNSAFE_ENUM_CAST and METHOD__JIT_HELPERS__UNSAFE_ENUM_CAST_LONG cases in getILIntrinsicImplementation
            if (t.IsEnum)
            {
                TypeCode underlyingTypeCode = Type.GetTypeCode(Enum.GetUnderlyingType(t));

                // Depending on the enum type, we need to special case the comparers so that we avoid boxing
                // Note: We have different comparers for Short and SByte because for those types we need to make sure we call GetHashCode on the actual underlying type as the
                // implementation of GetHashCode is more complex than for the other types.
                switch (underlyingTypeCode)
                {
                case TypeCode.Int16:     // short
#if MONO
                    return((EqualityComparer <T>)RuntimeType.CreateInstanceForAnotherGenericParameter(typeof(ShortEnumEqualityComparer <>), t));
#else
                    return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(ShortEnumEqualityComparer <short>), t));
#endif
                case TypeCode.SByte:
#if MONO
                    return((EqualityComparer <T>)RuntimeType.CreateInstanceForAnotherGenericParameter(typeof(SByteEnumEqualityComparer <>), t));
#else
                    return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(SByteEnumEqualityComparer <sbyte>), t));
#endif
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Byte:
                case TypeCode.UInt16:     //ushort
#if MONO
                    return((EqualityComparer <T>)RuntimeType.CreateInstanceForAnotherGenericParameter(typeof(EnumEqualityComparer <>), t));
#else
                    return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(EnumEqualityComparer <int>), t));
#endif
                case TypeCode.Int64:
                case TypeCode.UInt64:
#if MONO
                    return((EqualityComparer <T>)RuntimeType.CreateInstanceForAnotherGenericParameter(typeof(LongEnumEqualityComparer <>), t));
#else
                    return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(LongEnumEqualityComparer <long>), t));
#endif
                }
            }
            // Otherwise return an ObjectEqualityComparer<T>
            return(new ObjectEqualityComparer <T>());
        }