示例#1
0
        public sealed override bool IsEnumDefined(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (!IsEnum)
            {
                throw new ArgumentException(SR.Arg_MustBeEnum);
            }

            if (value is string valueAsString)
            {
                EnumInfo enumInfo = Enum.GetEnumInfo(this);
                foreach (string name in enumInfo.Names)
                {
                    if (valueAsString == name)
                    {
                        return(true);
                    }
                }
                return(false);
            }
            else
            {
                ulong rawValue;
                if (!Enum.TryGetUnboxedValueOfEnumOrInteger(value, out rawValue))
                {
                    if (Type.IsIntegerType(value.GetType()))
                    {
                        throw new ArgumentException(SR.Format(SR.Arg_EnumUnderlyingTypeAndObjectMustBeSameType, value.GetType(), Enum.InternalGetUnderlyingType(this)));
                    }
                    else
                    {
                        throw new InvalidOperationException(SR.InvalidOperation_UnknownEnumType);
                    }
                }

                if (value is Enum)
                {
                    if (!Enum.ValueTypeMatchesEnumType(this, value))
                    {
                        throw new ArgumentException(SR.Format(SR.Arg_EnumAndObjectMustBeSameType, value.GetType(), this));
                    }
                }
                else
                {
                    Type underlyingType = Enum.InternalGetUnderlyingType(this);
                    if (!(underlyingType.TypeHandle.ToEETypePtr() == value.EETypePtr))
                    {
                        throw new ArgumentException(SR.Format(SR.Arg_EnumUnderlyingTypeAndObjectMustBeSameType, value.GetType(), underlyingType));
                    }
                }

                return(Enum.GetEnumName(this, rawValue) != null);
            }
        }
示例#2
0
        public override string?GetEnumName(object value)
        {
            ArgumentNullException.ThrowIfNull(value);

            RuntimeType valueType = (RuntimeType)value.GetType();

            if (!(valueType.IsActualEnum || IsIntegerType(valueType)))
            {
                throw new ArgumentException(SR.Arg_MustBeEnumBaseTypeOrEnum, nameof(value));
            }

            ulong ulValue = Enum.ToUInt64(value);

            return(Enum.GetEnumName(this, ulValue));
        }
示例#3
0
文件: Enum.cs 项目: relaxar/.net
        private static string InternalFormat(RuntimeType eT, ulong value)
        {
            Debug.Assert(eT != null);

            // These values are sorted by value. Don't change this
            TypeValuesAndNames entry = GetCachedValuesAndNames(eT, true);

            if (!entry.IsFlag) // Not marked with Flags attribute
            {
                return(Enum.GetEnumName(eT, value));
            }
            else // These are flags OR'ed together (We treat everything as unsigned types)
            {
                return(InternalFlagsFormat(eT, entry, value));
            }
        }
示例#4
0
        public override string?GetEnumName(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            Type valueType = value.GetType();

            if (!(valueType.IsEnum || IsIntegerType(valueType)))
            {
                throw new ArgumentException(SR.Arg_MustBeEnumBaseTypeOrEnum, nameof(value));
            }

            ulong ulValue = Enum.ToUInt64(value);

            return(Enum.GetEnumName(this, ulValue));
        }
示例#5
0
        public sealed override string?GetEnumName(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            ulong rawValue;

            if (!Enum.TryGetUnboxedValueOfEnumOrInteger(value, out rawValue))
            {
                throw new ArgumentException(SR.Arg_MustBeEnumBaseTypeOrEnum, nameof(value));
            }

            // For desktop compatibility, do not bounce an incoming integer that's the wrong size.
            // Do a value-preserving cast of both it and the enum values and do a 64-bit compare.

            if (!IsEnum)
            {
                throw new ArgumentException(SR.Arg_MustBeEnum);
            }

            return(Enum.GetEnumName(this, rawValue));
        }