Exemple #1
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))
            {
                return((Comparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericComparer <int>), 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>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableComparer <int>), u));
                }
            }
            // Otherwise return an ObjectComparer<T>
            return(new ObjectComparer <T>());
        }
Exemple #2
0
        //
        // Note that logic in this method is replicated in vm\compile.cpp to ensure that NGen
        // saves the right instantiations
        //
        private static Comparer <T> CreateComparer()
        {
            object      result = null;
            RuntimeType t      = (RuntimeType)typeof(T);

            // If T implements IComparable<T> return a GenericComparer<T>
            if (typeof(IComparable <T>).IsAssignableFrom(t))
            {
                result = RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericComparer <int>), t);
            }
            else if (default(T) == null)
            {
                // 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))
                    {
                        result = RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableComparer <int>), u);
                    }
                }
            }
            else if (t.IsEnum)
            {
                // Explicitly call Enum.GetUnderlyingType here. Although GetTypeCode
                // ends up doing this anyway, we end up avoiding an unnecessary P/Invoke
                // and virtual method call.
                TypeCode underlyingTypeCode = Type.GetTypeCode(Enum.GetUnderlyingType(t));

                // Depending on the enum type, we need to special case the comparers so that we avoid boxing
                // Specialize differently for signed/unsigned types so we avoid problems with large numbers
                switch (underlyingTypeCode)
                {
                case TypeCode.SByte:
                case TypeCode.Int16:
                case TypeCode.Int32:
                    result = RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(Int32EnumComparer <int>), t);
                    break;

                case TypeCode.Byte:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                    result = RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(UInt32EnumComparer <uint>), t);
                    break;

                // 64-bit enums: use UnsafeEnumCastLong
                case TypeCode.Int64:
                    result = RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(Int64EnumComparer <long>), t);
                    break;

                case TypeCode.UInt64:
                    result = RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(UInt64EnumComparer <ulong>), t);
                    break;
                }
            }

            return(result != null ?
                   (Comparer <T>)result :
                   new ObjectComparer <T>()); // Fallback to ObjectComparer, which uses boxing
        }
        private static EqualityComparer <T> CreateComparer()
        {
            RuntimeType c = (RuntimeType)typeof(T);

            if (c == typeof(byte))
            {
                return((EqualityComparer <T>) new ByteEqualityComparer());
            }
            if (typeof(IEquatable <T>).IsAssignableFrom(c))
            {
                return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericEqualityComparer <int>), c));
            }
            if (c.IsGenericType && (c.GetGenericTypeDefinition() == typeof(Nullable <>)))
            {
                RuntimeType type2 = (RuntimeType)c.GetGenericArguments()[0];
                if (typeof(IEquatable <>).MakeGenericType(new Type[] { type2 }).IsAssignableFrom(type2))
                {
                    return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableEqualityComparer <int>), type2));
                }
            }
            if (c.IsEnum && (Enum.GetUnderlyingType(c) == typeof(int)))
            {
                return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(EnumEqualityComparer <int>), c));
            }
            return(new ObjectEqualityComparer <T>());
        }
Exemple #4
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> ());
        }
Exemple #5
0
        [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 T implements IEquatable<T> return a GenericEqualityComparer<T>
            if (typeof(IEquatable <T>).IsAssignableFrom(t))
            {
                return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericEqualityComparer <int>), t));
            }
            // 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))
                {
                    return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableEqualityComparer <int>), u));
                }
            }
            // 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))
            {
                return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(EnumEqualityComparer <int>), t));
            }
            // Otherwise return an ObjectEqualityComparer<T>
            return(new ObjectEqualityComparer <T>());
        }
Exemple #6
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        private static Comparer <T> CreateComparer()
        {
            object      result = null;
            RuntimeType t      = (RuntimeType)typeof(T);

            // If T implements IComparable<T> return a GenericComparer<T>
            if (typeof(IComparable <T>).IsAssignableFrom(t))
            {
                result = RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericComparer <int>), t);
            }
            else if (default(T) == null)
            {
                // 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))
                    {
                        result = RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableComparer <int>), u);
                    }
                }
            }

            return(result != null ?
                   (Comparer <T>)result :
                   new ObjectComparer <T>()); // Fallback to ObjectComparer, which uses boxing
        }
        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>());
        }
Exemple #8
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        private static EqualityComparer <T> CreateComparer()
        {
            Contract.Ensures(Contract.Result <EqualityComparer <T> >() != null);

            RuntimeType t = (RuntimeType)typeof(T);//获取泛型格式

            // 用于性能原因的专门类型字节
            if (t == typeof(byte))
            {
                return((EqualityComparer <T>)(object)(new ByteEqualityComparer()));
            }
            // If T implements IEquatable<T> return a GenericEqualityComparer<T>
            // 如果T实现IEquatable<T>,则返回GenericEqualityComparer<T>
            if (typeof(IEquatable <T>).IsAssignableFrom(t))
            {
                return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericEqualityComparer <int>), t));
            }
            // If T is a Nullable<U> where U implements IEquatable<U> return a NullableEqualityComparer<U>
            // 如果T是一个实现了IEquatable<U>的Nullable<U>,将返回一个NullableEqualityComparer<U>
            if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                RuntimeType u = (RuntimeType)t.GetGenericArguments()[0];
                if (typeof(IEquatable <>).MakeGenericType(u).IsAssignableFrom(u))
                {
                    return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableEqualityComparer <int>), u));
                }
            }

            // 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
                    return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(ShortEnumEqualityComparer <short>), t));

                case TypeCode.SByte:
                    return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(SByteEnumEqualityComparer <sbyte>), t));

                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Byte:
                case TypeCode.UInt16:     //ushort
                    return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(EnumEqualityComparer <int>), t));

                case TypeCode.Int64:
                case TypeCode.UInt64:
                    return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(LongEnumEqualityComparer <long>), t));
                }
            }
            // Otherwise return an ObjectEqualityComparer<T>
            return(new ObjectEqualityComparer <T>());
        }
        /// <summary>
        /// Creates the default <see cref="Comparer{T}"/> for a nullable type.
        /// </summary>
        /// <param name="nullableType">The nullable type to create the default comparer for.</param>
        private static object TryCreateNullableComparer(RuntimeType nullableType)
        {
            Debug.Assert(nullableType != null);
            Debug.Assert(nullableType.IsGenericType && nullableType.GetGenericTypeDefinition() == typeof(Nullable <>));

            var embeddedType = (RuntimeType)nullableType.GetGenericArguments()[0];

            if (typeof(IComparable <>).MakeGenericType(embeddedType).IsAssignableFrom(embeddedType))
            {
                return(RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableComparer <int>), embeddedType));
            }

            return(null);
        }
        internal static Attribute[] GetCustomAttributes(RuntimeType type, Type caType, bool includeSecCa, out int count)
        {
            count = 0;
            bool flag = (caType == typeof(object)) || (caType == typeof(Attribute));

            if ((!flag && (s_pca[caType] == null)) && !IsSecurityAttribute(caType))
            {
                return(new Attribute[0]);
            }
            List <Attribute> list = new List <Attribute>();
            Attribute        item = null;

            if (flag || (caType == typeof(SerializableAttribute)))
            {
                item = SerializableAttribute.GetCustomAttribute(type);
                if (item != null)
                {
                    list.Add(item);
                }
            }
            if (flag || (caType == typeof(ComImportAttribute)))
            {
                item = ComImportAttribute.GetCustomAttribute(type);
                if (item != null)
                {
                    list.Add(item);
                }
            }
            if ((includeSecCa && (flag || IsSecurityAttribute(caType))) && !type.IsGenericParameter)
            {
                object[] objArray;
                if (type.IsGenericType)
                {
                    type = (RuntimeType)type.GetGenericTypeDefinition();
                }
                GetSecurityAttributes(type.Module.ModuleHandle, type.MetadataToken, out objArray);
                if (objArray != null)
                {
                    foreach (object obj2 in objArray)
                    {
                        if ((caType == obj2.GetType()) || obj2.GetType().IsSubclassOf(caType))
                        {
                            list.Add((Attribute)obj2);
                        }
                    }
                }
            }
            count = list.Count;
            return(list.ToArray());
        }
 internal static object[] GetCustomAttributes(RuntimeType type, RuntimeType caType, bool inherit)
 {
     if (type.GetElementType() != null)
     {
         if (!caType.IsValueType)
         {
             return(CustomAttribute.CreateAttributeArrayHelper(caType, 0));
         }
         return(EmptyArray <object> .Value);
     }
     else
     {
         if (type.IsGenericType && !type.IsGenericTypeDefinition)
         {
             type = (type.GetGenericTypeDefinition() as RuntimeType);
         }
         int         i = 0;
         Attribute[] customAttributes = PseudoCustomAttribute.GetCustomAttributes(type, caType, true, out i);
         if (!inherit || (caType.IsSealed && !CustomAttribute.GetAttributeUsage(caType).Inherited))
         {
             object[] customAttributes2 = CustomAttribute.GetCustomAttributes(type.GetRuntimeModule(), type.MetadataToken, i, caType, !CustomAttribute.AllowCriticalCustomAttributes(type));
             if (i > 0)
             {
                 Array.Copy(customAttributes, 0, customAttributes2, customAttributes2.Length - i, i);
             }
             return(customAttributes2);
         }
         List <object> list = new List <object>();
         bool          mustBeInheritable = false;
         Type          elementType       = (caType == null || caType.IsValueType || caType.ContainsGenericParameters) ? typeof(object) : caType;
         while (i > 0)
         {
             list.Add(customAttributes[--i]);
         }
         while (type != (RuntimeType)typeof(object) && type != null)
         {
             object[] customAttributes3 = CustomAttribute.GetCustomAttributes(type.GetRuntimeModule(), type.MetadataToken, 0, caType, mustBeInheritable, list, !CustomAttribute.AllowCriticalCustomAttributes(type));
             mustBeInheritable = true;
             for (int j = 0; j < customAttributes3.Length; j++)
             {
                 list.Add(customAttributes3[j]);
             }
             type = (type.BaseType as RuntimeType);
         }
         object[] array = CustomAttribute.CreateAttributeArrayHelper(elementType, list.Count);
         Array.Copy(list.ToArray(), 0, array, 0, list.Count);
         return(array);
     }
 }
Exemple #12
0
        internal static Attribute[] GetCustomAttributes(RuntimeType type, RuntimeType caType, bool includeSecCa, out int count)
        {
            count = 0;
            bool flag = caType == (RuntimeType)typeof(object) || caType == (RuntimeType)typeof(Attribute);

            if (!flag && PseudoCustomAttribute.s_pca.GetValueOrDefault(caType) == null && !PseudoCustomAttribute.IsSecurityAttribute(caType))
            {
                return(new Attribute[0]);
            }
            List <Attribute> list = new List <Attribute>();

            if (flag || caType == (RuntimeType)typeof(SerializableAttribute))
            {
                Attribute customAttribute = SerializableAttribute.GetCustomAttribute(type);
                if (customAttribute != null)
                {
                    list.Add(customAttribute);
                }
            }
            if (flag || caType == (RuntimeType)typeof(ComImportAttribute))
            {
                Attribute customAttribute = ComImportAttribute.GetCustomAttribute(type);
                if (customAttribute != null)
                {
                    list.Add(customAttribute);
                }
            }
            if (includeSecCa && (flag || PseudoCustomAttribute.IsSecurityAttribute(caType)) && !type.IsGenericParameter && type.GetElementType() == null)
            {
                if (type.IsGenericType)
                {
                    type = (RuntimeType)type.GetGenericTypeDefinition();
                }
                object[] array;
                PseudoCustomAttribute.GetSecurityAttributes(type.Module.ModuleHandle.GetRuntimeModule(), type.MetadataToken, false, out array);
                if (array != null)
                {
                    foreach (object obj in array)
                    {
                        if (caType == obj.GetType() || obj.GetType().IsSubclassOf(caType))
                        {
                            list.Add((Attribute)obj);
                        }
                    }
                }
            }
            count = list.Count;
            return(list.ToArray());
        }
        internal static object[] GetCustomAttributes(RuntimeType type, RuntimeType caType, bool inherit)
        {
            if (type.GetElementType() != null)
            {
                if (!caType.IsValueType)
                {
                    return(CreateAttributeArrayHelper(caType, 0));
                }
                return(new object[0]);
            }
            if (type.IsGenericType && !type.IsGenericTypeDefinition)
            {
                type = type.GetGenericTypeDefinition() as RuntimeType;
            }
            int count = 0;

            Attribute[] sourceArray = PseudoCustomAttribute.GetCustomAttributes(type, caType, true, out count);
            if (!inherit || (caType.IsSealed && !GetAttributeUsage(caType).Inherited))
            {
                object[] objArray = GetCustomAttributes(type.GetRuntimeModule(), type.MetadataToken, count, caType, !AllowCriticalCustomAttributes(type));
                if (count > 0)
                {
                    Array.Copy(sourceArray, 0, objArray, objArray.Length - count, count);
                }
                return(objArray);
            }
            List <object> derivedAttributes = new List <object>();
            bool          mustBeInheritable = false;
            Type          elementType       = (((caType == null) || caType.IsValueType) || caType.ContainsGenericParameters) ? typeof(object) : caType;

            while (count > 0)
            {
                derivedAttributes.Add(sourceArray[--count]);
            }
            while ((type != ((RuntimeType)typeof(object))) && (type != null))
            {
                object[] objArray2 = GetCustomAttributes(type.GetRuntimeModule(), type.MetadataToken, 0, caType, mustBeInheritable, derivedAttributes, !AllowCriticalCustomAttributes(type));
                mustBeInheritable = true;
                for (int i = 0; i < objArray2.Length; i++)
                {
                    derivedAttributes.Add(objArray2[i]);
                }
                type = type.BaseType as RuntimeType;
            }
            object[] destinationArray = CreateAttributeArrayHelper(elementType, derivedAttributes.Count);
            Array.Copy(derivedAttributes.ToArray(), 0, destinationArray, 0, derivedAttributes.Count);
            return(destinationArray);
        }
Exemple #14
0
        internal static object[] GetCustomAttributes(RuntimeType type, RuntimeType caType, bool inherit)
        {
            if (type.GetElementType() != (Type)null)
            {
                if (!caType.IsValueType)
                {
                    return(CustomAttribute.CreateAttributeArrayHelper((Type)caType, 0));
                }
                return(EmptyArray <object> .Value);
            }
            if (type.IsGenericType && !type.IsGenericTypeDefinition)
            {
                type = type.GetGenericTypeDefinition() as RuntimeType;
            }
            int count = 0;

            Attribute[] customAttributes1 = PseudoCustomAttribute.GetCustomAttributes(type, caType, true, out count);
            if (!inherit || caType.IsSealed && !CustomAttribute.GetAttributeUsage(caType).Inherited)
            {
                object[] customAttributes2 = CustomAttribute.GetCustomAttributes(type.GetRuntimeModule(), type.MetadataToken, count, caType, !CustomAttribute.AllowCriticalCustomAttributes(type));
                if (count > 0)
                {
                    Array.Copy((Array)customAttributes1, 0, (Array)customAttributes2, customAttributes2.Length - count, count);
                }
                return(customAttributes2);
            }
            List <object> objectList        = new List <object>();
            bool          mustBeInheritable = false;
            Type          elementType       = (caType == (RuntimeType)null || caType.IsValueType ? 1 : (caType.ContainsGenericParameters ? 1 : 0)) != 0 ? typeof(object) : (Type)caType;

            while (count > 0)
            {
                objectList.Add((object)customAttributes1[--count]);
            }
            for (; type != (RuntimeType)typeof(object) && type != (RuntimeType)null; type = type.BaseType as RuntimeType)
            {
                object[] customAttributes2 = CustomAttribute.GetCustomAttributes(type.GetRuntimeModule(), type.MetadataToken, 0, caType, mustBeInheritable, (IList)objectList, !CustomAttribute.AllowCriticalCustomAttributes(type));
                mustBeInheritable = true;
                for (int index = 0; index < customAttributes2.Length; ++index)
                {
                    objectList.Add(customAttributes2[index]);
                }
            }
            object[] attributeArrayHelper = CustomAttribute.CreateAttributeArrayHelper(elementType, objectList.Count);
            Array.Copy((Array)objectList.ToArray(), 0, (Array)attributeArrayHelper, 0, objectList.Count);
            return(attributeArrayHelper);
        }
        private static EqualityComparer <T> CreateComparer()
        {
            RuntimeType runtimeType = (RuntimeType)typeof(T);

            if (runtimeType == typeof(byte))
            {
                return((EqualityComparer <T>) new ByteEqualityComparer());
            }
            if (typeof(IEquatable <T>).IsAssignableFrom(runtimeType))
            {
                return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericEqualityComparer <int>), runtimeType));
            }
            if (runtimeType.IsGenericType && runtimeType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                RuntimeType runtimeType2 = (RuntimeType)runtimeType.GetGenericArguments()[0];
                if (typeof(IEquatable <>).MakeGenericType(new Type[]
                {
                    runtimeType2
                }).IsAssignableFrom(runtimeType2))
                {
                    return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableEqualityComparer <int>), runtimeType2));
                }
            }
            if (runtimeType.IsEnum)
            {
                switch (Type.GetTypeCode(Enum.GetUnderlyingType(runtimeType)))
                {
                case TypeCode.SByte:
                    return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(SByteEnumEqualityComparer <sbyte>), runtimeType));

                case TypeCode.Byte:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                    return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(EnumEqualityComparer <int>), runtimeType));

                case TypeCode.Int16:
                    return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(ShortEnumEqualityComparer <short>), runtimeType));

                case TypeCode.Int64:
                case TypeCode.UInt64:
                    return((EqualityComparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(LongEnumEqualityComparer <long>), runtimeType));
                }
            }
            return(new ObjectEqualityComparer <T>());
        }
Exemple #16
0
        internal virtual QName GetDictionaryQName()
        {
            string name = a != null ? a.Name : null;
            string ns   = a != null ? a.Namespace : null;

            if (RuntimeType.IsGenericType && RuntimeType.GetGenericTypeDefinition() != typeof(Dictionary <,>))
            {
                name = name ?? KnownTypeCollection.GetDefaultName(RuntimeType);
            }
            else
            {
                name = "ArrayOf" + GetItemQName().Name;
            }
            ns = ns ?? KnownTypeCollection.MSArraysNamespace;

            return(new QName(name, ns));
        }
Exemple #17
0
        private static Comparer <T> CreateComparer()
        {
            RuntimeType genericParameter1 = (RuntimeType)typeof(T);

            if (typeof(IComparable <T>).IsAssignableFrom((Type)genericParameter1))
            {
                return((Comparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericComparer <int>), genericParameter1));
            }
            if (genericParameter1.IsGenericType && genericParameter1.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                RuntimeType genericParameter2 = (RuntimeType)genericParameter1.GetGenericArguments()[0];
                if (typeof(IComparable <>).MakeGenericType((Type)genericParameter2).IsAssignableFrom((Type)genericParameter2))
                {
                    return((Comparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableComparer <int>), genericParameter2));
                }
            }
            return((Comparer <T>) new ObjectComparer <T>());
        }
Exemple #18
0
        private static Comparer <T> CreateComparer()
        {
            RuntimeType c = (RuntimeType)typeof(T);

            if (typeof(IComparable <T>).IsAssignableFrom(c))
            {
                return((Comparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericComparer <int>), c));
            }
            if (c.IsGenericType && (c.GetGenericTypeDefinition() == typeof(Nullable <>)))
            {
                RuntimeType type2 = (RuntimeType)c.GetGenericArguments()[0];
                if (typeof(IComparable <>).MakeGenericType(new Type[] { type2 }).IsAssignableFrom(type2))
                {
                    return((Comparer <T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableComparer <int>), type2));
                }
            }
            return(new ObjectComparer <T>());
        }
Exemple #19
0
        object CreateInstance()
        {
            if (RuntimeType.IsInterface)
            {
                if (RuntimeType.IsGenericType && Array.IndexOf(RuntimeType.GetGenericTypeDefinition().GetInterfaces(), typeof(IDictionary <,>)) >= 0)
                {
                    var gargs = RuntimeType.GetGenericArguments();
                    return(Activator.CreateInstance(typeof(Dictionary <,>).MakeGenericType(gargs [0], gargs [1]))); // Dictionary<T>
                }
                else                                                                                                // non-generic
                {
                    return(new Hashtable());
                }
            }
#if NET_2_1 // FIXME: is it fine?
            return(Activator.CreateInstance(RuntimeType));
#else
            return(Activator.CreateInstance(RuntimeType, true));
#endif
        }
Exemple #20
0
        private static String NonQualifiedTypeName(this Type type)
        {
            RuntimeType runtimeType = type as RuntimeType;

            if (runtimeType == null)
            {
                return(null);
            }

            if (runtimeType.HasElementType)
            {
                String elementTypeName = runtimeType.InternalRuntimeElementType.NonQualifiedTypeName();
                if (elementTypeName == null)
                {
                    return(null);
                }
                String suffix;
                if (runtimeType.IsArray)
                {
                    int rank = runtimeType.GetArrayRank();
                    if (rank == 1)
                    {
                        suffix = "[" + (runtimeType.InternalIsMultiDimArray ? "*" : "") + "]";
                    }
                    else
                    {
                        suffix = "[" + new String(',', rank - 1) + "]";
                    }
                }
                else if (runtimeType.IsByRef)
                {
                    suffix = "&";
                }
                else if (runtimeType.IsPointer)
                {
                    suffix = "*";
                }
                else
                {
                    return(null);
                }

                return(elementTypeName + suffix);
            }
            else if (runtimeType.IsGenericParameter)
            {
                return(null);
            }
            else if (runtimeType.IsConstructedGenericType)
            {
                StringBuilder sb = new StringBuilder();
                String        genericTypeDefinitionTypeName = runtimeType.GetGenericTypeDefinition().NonQualifiedTypeName();
                if (genericTypeDefinitionTypeName == null)
                {
                    return(null);
                }
                sb.Append(genericTypeDefinitionTypeName);
                sb.Append("[");
                String sep = "";
                foreach (RuntimeType ga in runtimeType.InternalRuntimeGenericTypeArguments)
                {
                    String gaTypeName = ga.AssemblyQualifiedTypeName();
                    if (gaTypeName == null)
                    {
                        return(null);
                    }
                    sb.Append(sep + "[" + gaTypeName + "]");
                    sep = ",";
                }
                sb.Append("]");

                return(sb.ToString());
            }
            else
            {
                RuntimeNamedTypeInfo runtimeNamedTypeInfo = type.GetTypeInfo() as RuntimeNamedTypeInfo;
                if (runtimeNamedTypeInfo == null)
                {
                    return(null);
                }
                MetadataReader reader = runtimeNamedTypeInfo.Reader;

                String s = "";
                TypeDefinitionHandle      typeDefinitionHandle = runtimeNamedTypeInfo.TypeDefinitionHandle;
                NamespaceDefinitionHandle namespaceDefinitionHandle;
                do
                {
                    TypeDefinition typeDefinition = typeDefinitionHandle.GetTypeDefinition(reader);
                    String         name           = typeDefinition.Name.GetString(reader);
                    if (s == "")
                    {
                        s = name;
                    }
                    else
                    {
                        s = name + "+" + s;
                    }
                    namespaceDefinitionHandle = typeDefinition.NamespaceDefinition;
                    typeDefinitionHandle      = typeDefinition.EnclosingType;
                }while (!typeDefinitionHandle.IsNull(reader));

                NamespaceChain namespaceChain = new NamespaceChain(reader, namespaceDefinitionHandle);
                String         ns             = namespaceChain.NameSpace;
                if (ns != null)
                {
                    s = ns + "." + s;
                }
                return(s);
            }
        }
        [System.Security.SecuritySafeCritical]  // auto-generated
        private static EqualityComparer <T> CreateComparer()
        {
            Contract.Ensures(Contract.Result <EqualityComparer <T> >() != null);

            object      result = null;
            RuntimeType t      = (RuntimeType)typeof(T);

            // Specialize type byte for performance reasons
            if (t == typeof(byte))
            {
                result = new ByteEqualityComparer();
            }
            // If T implements IEquatable<T> return a GenericEqualityComparer<T>
            else if (typeof(IEquatable <T>).IsAssignableFrom(t))
            {
                result = RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericEqualityComparer <int>), t);
            }
            else if (default(T) == null) // Reference type/Nullable
            {
                // 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))
                    {
                        result = RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableEqualityComparer <int>), u);
                    }
                }
            }
            // See the METHOD__JIT_HELPERS__UNSAFE_ENUM_CAST and METHOD__JIT_HELPERS__UNSAFE_ENUM_CAST_LONG cases in getILIntrinsicImplementation
            else 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
                    result = RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(ShortEnumEqualityComparer <short>), t);
                    break;

                case TypeCode.SByte:
                    result = RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(SByteEnumEqualityComparer <sbyte>), t);
                    break;

                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Byte:
                case TypeCode.UInt16:     //ushort
                    result = RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(EnumEqualityComparer <int>), t);
                    break;

                case TypeCode.Int64:
                case TypeCode.UInt64:
                    result = RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(LongEnumEqualityComparer <long>), t);
                    break;
                }
            }

            return(result != null ?
                   (EqualityComparer <T>)result :
                   new ObjectEqualityComparer <T>()); // Fallback to ObjectEqualityComparer, which uses boxing
        }
        [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>());
        }