Beispiel #1
0
        public static string GetName(Type enumType, object value)
        {
            if (enumType == null)
            {
                throw new ArgumentNullException("enumType");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (!enumType.IsEnum)
            {
                throw new ArgumentException("enumType is not an Enum type.", "enumType");
            }

            MonoEnumInfo info;

            MonoEnumInfo.GetInfo(enumType, out info);

            if (enumType is MonoType)
            {
                value = ToObject(enumType, value);
            }
            else
            {
                value = MonoEnumInfo.ToUInt64(value);
            }

            int i = FindPosition(info.utype, value, info.values);

            return((i >= 0) ? info.names [i] : null);
        }
Beispiel #2
0
        public static bool IsDefined(Type enumType, object value)
        {
            if (enumType == null)
            {
                throw new ArgumentNullException("enumType");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (!enumType.IsEnum)
            {
                throw new ArgumentException("enumType is not an Enum type.", "enumType");
            }

            MonoEnumInfo info;

            MonoEnumInfo.GetInfo(enumType, out info);

            Type vType = value.GetType();

            if (vType == typeof(String))
            {
                return(((IList)(info.names)).Contains(value));
            }

            if ((vType == info.utype || !(enumType is MonoType)) || (vType == enumType))
            {
                if (enumType is MonoType)
                {
                    value = ToObject(enumType, value);
                }
                else
                {
                    value = MonoEnumInfo.ToUInt64(value);
                }

                MonoEnumInfo.GetInfo(enumType, out info);
                return(FindPosition(info.utype, value, info.values) >= 0);
            }

            if (IsValidEnumType(info.utype))
            {
                throw new ArgumentException("The value parameter is not the correct type. "
                                            + "It must be type String or the same type as the underlying type "
                                            + "of the Enum.");
            }

            throw new InvalidOperationException("Unknown enum type.");
        }