System.Enum.InternalFlagsFormat C# (CSharp) Метод

InternalFlagsFormat() приватный статический Метод

private static InternalFlagsFormat ( Type eT, Object value ) : String
eT Type
value Object
Результат String
        private static String InternalFlagsFormat(Type eT, Object value)
        {
            ulong result = ToUInt64(value);
            HashEntry hashEntry = GetHashEntry(eT);
            // These values are sorted by value. Don't change this
            String [] names = hashEntry.names;
            ulong [] values = hashEntry.values;

            int index = values.Length - 1;
            StringBuilder retval = new StringBuilder();
            bool firstTime = true;
            ulong saveResult = result;

            // We will not optimize this code further to keep it maintainable. There are some boundary checks that can be applied
            // to minimize the comparsions required. This code works the same for the best/worst case. In general the number of
            // items in an enum are sufficiently small and not worth the optimization.
            while (index >= 0)
            {
                if ((index == 0) && (values[index] == 0))
                    break;

                if ((result & values[index]) == values[index])
                {
                    result -= values[index];
                    if (!firstTime)
                        retval.Insert(0, enumSeperator);

                    retval.Insert(0, names[index]);
                    firstTime = false;
                }

                index--;
            }

            // We were unable to represent this number as a bitwise or of valid flags
            if (result != 0)
                return value.ToString();

            // For the case when we have zero
            if (saveResult==0)
            {
                if (values[0] == 0)
                    return names[0]; // Zero was one of the enum values.
                else
                    return "0";
            }
            else
            return retval.ToString(); // Return the string representation
        }

Usage Example

Пример #1
0
        public static string Format(Type enumType, object value, string format)
        {
            if (enumType == null)
            {
                throw new ArgumentNullException(nameof(enumType));
            }

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

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            RuntimeType rtType = enumType as RuntimeType;

            if (rtType == null)
            {
                throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType));
            }

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

            Type underlyingType = GetUnderlyingType(enumType);

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

                if (format.Length != 1)
                {
                    // all acceptable format string are of length 1
                    throw new FormatException(SR.Format_InvalidEnumFormatSpecification);
                }
                return(((Enum)value).ToString(format));
            }
            // The value must be of the same type as the Underlying type of the Enum
            else if (valueType != underlyingType)
            {
                throw new ArgumentException(SR.Format(SR.Arg_EnumFormatUnderlyingTypeAndObjectMustBeSameType, valueType.ToString(), underlyingType.ToString()));
            }
            if (format.Length != 1)
            {
                // all acceptable format string are of length 1
                throw new FormatException(SR.Format_InvalidEnumFormatSpecification);
            }

            char formatCh = format[0];

            if (formatCh == 'G' || formatCh == 'g')
            {
                return(GetEnumName(rtType, ToUInt64(value)) ?? value.ToString());
            }

            if (formatCh == 'D' || formatCh == 'd')
            {
                return(value.ToString());
            }

            if (formatCh == 'X' || formatCh == 'x')
            {
                return(InternalFormattedHexString(value));
            }

            if (formatCh == 'F' || formatCh == 'f')
            {
                return(Enum.InternalFlagsFormat(rtType, ToUInt64(value)) ?? value.ToString());
            }

            throw new FormatException(SR.Format_InvalidEnumFormatSpecification);
        }