Ejemplo n.º 1
0
        public static bool IsEnum(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            return(TypeProvider.IsSubclassOf(type, typeof(Enum)));
        }
Ejemplo n.º 2
0
        public static string[] GetEnumNames(Type enumType)
        {
            if (enumType == null)
            {
                throw new ArgumentNullException("enumType");
            }

            if (!TypeProvider.IsSubclassOf(enumType, typeof(Enum)))
            {
                throw new ArgumentException(TypeSystemSR.GetString("Error_TypeIsNotEnum"));
            }

            FieldInfo[] flds = enumType.GetFields();

            List <string> names = new List <String>();

            for (int i = 0; i < flds.Length; i++)
            {
                names.Add(flds[i].Name);
            }

            names.Sort();
            return(names.ToArray());
        }
Ejemplo n.º 3
0
        public static Type GetEventHandlerType(EventInfo eventInfo)
        {
            if (eventInfo == null)
            {
                throw new ArgumentNullException("eventInfo");
            }

            MethodInfo m = eventInfo.GetAddMethod(true);

            if (m != null)
            {
                ParameterInfo[] p   = m.GetParameters();
                Type            del = typeof(Delegate);
                for (int i = 0; i < p.Length; i++)
                {
                    Type c = p[i].ParameterType;
                    if (TypeProvider.IsSubclassOf(c, del))
                    {
                        return(c);
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 4
0
        internal static bool IsAssignable(Type toType, Type fromType, bool equalBasedOnSameTypeRepresenting)
        {
            if (toType == null || fromType == null)
            {
                return(false);
            }

            if (equalBasedOnSameTypeRepresenting)
            {
                if (IsRepresentingTheSameType(fromType, toType))
                {
                    return(true);
                }
            }
            else
            {
                if (fromType == toType)
                {
                    return(true);
                }
            }

            if (toType.IsGenericTypeDefinition)
            {
                return(toType.IsAssignableFrom(fromType));
            }

            // runtime type can never be assigned to design time type
            if (toType.Assembly == null && fromType.Assembly != null)
            {
                return(false);
            }
            if (fromType is RTTypeWrapper || fromType is DesignTimeType)
            {
                if (!(toType is RTTypeWrapper) && !(toType is DesignTimeType))
                {
#pragma warning suppress 56506
                    ITypeProvider provider = fromType is RTTypeWrapper ? (fromType as RTTypeWrapper).Provider : (fromType as DesignTimeType).Provider;
                    if (provider != null)
                    {
                        toType = provider.GetType(toType.FullName);
                    }
                }
            }
            else if (toType is RTTypeWrapper || toType is DesignTimeType)
            {
                if (!(fromType is RTTypeWrapper) && !(fromType is DesignTimeType))
                {
#pragma warning suppress 56506
                    ITypeProvider provider = toType is RTTypeWrapper ? (toType as RTTypeWrapper).Provider : (toType as DesignTimeType).Provider;
                    if (provider != null)
                    {
                        fromType = provider.GetType(fromType.FullName);
                    }
                }
            }
            else
            {
                return(toType.IsAssignableFrom(fromType));
            }

            //We need to check not null as there might be cases in which to and from types may not be found
            if (toType == null || fromType == null)
            {
                return(false);
            }

            if (equalBasedOnSameTypeRepresenting)
            {
                if (IsRepresentingTheSameType(fromType, toType))
                {
                    return(true);
                }
            }
            else
            {
                if (fromType == toType)
                {
                    return(true);
                }
            }

            if (TypeProvider.IsSubclassOf(fromType, toType))
            {
                return(true);
            }

            if (toType.IsInterface == false)
            {
                return(false);
            }

            Type[] interfaces = fromType.GetInterfaces();

            for (int i = 0; i < interfaces.Length; i++)
            {
                // unfortunately, IsSubclassOf does not cover the case when they are the same type.
                if (interfaces[i] == toType)
                {
                    return(true);
                }

                if (TypeProvider.IsSubclassOf(interfaces[i], toType))
                {
                    return(true);
                }
            }
            return(false);
        }