示例#1
0
        public override string[] GetEnumNames()
        {
            if (!IsActualEnum)
            {
                throw new ArgumentException(SR.Arg_MustBeEnum, "enumType");
            }

            string[] ret = Enum.InternalGetNames(this);

            // Make a copy since we can't hand out the same array since users can modify them
            return(new ReadOnlySpan <string>(ret).ToArray());
        }
示例#2
0
        public override bool IsEnumDefined(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

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

            // Check if both of them are of the same type
            RuntimeType valueType = (RuntimeType)value.GetType();

            // If the value is an Enum then we need to extract the underlying value from it
            if (valueType.IsEnum)
            {
                if (!valueType.IsEquivalentTo(this))
                {
                    throw new ArgumentException(SR.Format(SR.Arg_EnumAndObjectMustBeSameType, valueType, this));
                }

                valueType = (RuntimeType)valueType.GetEnumUnderlyingType();
            }

            // If a string is passed in
            if (valueType == StringType)
            {
                // Get all of the Fields, calling GetHashEntry directly to avoid copying
                string[] names = Enum.InternalGetNames(this);
                return(Array.IndexOf(names, value) >= 0);
            }

            // If an enum or integer value is passed in
            if (IsIntegerType(valueType))
            {
                RuntimeType underlyingType = Enum.InternalGetUnderlyingType(this);
                if (underlyingType != valueType)
                {
                    throw new ArgumentException(SR.Format(SR.Arg_EnumUnderlyingTypeAndObjectMustBeSameType, valueType, underlyingType));
                }

                ulong[] ulValues = Enum.InternalGetValues(this);
                ulong   ulValue  = Enum.ToUInt64(value);

                return(Array.BinarySearch(ulValues, ulValue) >= 0);
            }
            else
            {
                throw new InvalidOperationException(SR.InvalidOperation_UnknownEnumType);
            }
        }
示例#3
0
文件: Enum.cs 项目: relaxar/.net
        internal static string GetEnumName(RuntimeType eT, ulong ulValue)
        {
            Debug.Assert(eT != null);
            ulong[] ulValues = Enum.InternalGetValues(eT);
            int     index    = Array.BinarySearch(ulValues, ulValue);

            if (index >= 0)
            {
                string[] names = Enum.InternalGetNames(eT);
                return(names[index]);
            }

            return(null); // return null so the caller knows to .ToString() the input
        }