System.Enum.Format C# (CSharp) Method

Format() private method

private Format ( Type enumType, Object value, String format ) : String
enumType Type
value Object
format String
return String
        public static String Format(Type enumType, Object value, String format)
        {
            if (enumType == null)
                throw new ArgumentNullException("enumType");

            if (!(enumType is RuntimeType))
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");

            if (!enumType.IsEnum)
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");

            if (value == null)
                throw new ArgumentNullException("value");

            if (format == null)
                throw new ArgumentNullException("format");

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

            if (!(valueType is RuntimeType))
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "valueType");

            Type underlyingType = GetUnderlyingType(enumType);

            // If the value is an Enum then we need to extract the underlying value from it
            if (valueType.IsEnum) {
                Type valueUnderlyingType = GetUnderlyingType(valueType);

                if (valueType != enumType)
                    throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Arg_EnumAndObjectMustBeSameType"), valueType.ToString(), enumType.ToString()));

                valueType = valueUnderlyingType;
                value = ((Enum)value).GetValue();
            }
            // The value must be of the same type as the Underlying type of the Enum
            else if (valueType != underlyingType) {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Arg_EnumFormatUnderlyingTypeAndObjectMustBeSameType"), valueType.ToString(), underlyingType.ToString()));
            }

            if( format.Length != 1) {
                // all acceptable format string are of length 1
                throw new FormatException(Environment.GetResourceString("Format_InvalidEnumFormatSpecification"));
            }
            
            char formatCh = format[0];

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

            if (formatCh == 'X' || formatCh == 'x') {
                // Retrieve the value from the field.
                return InternalFormattedHexString(value);
            }

            if (formatCh == 'G' || formatCh == 'g') {
                return InternalFormat(enumType, value);
            }

            if (formatCh == 'F' || formatCh == 'f') {
                return InternalFlagsFormat(enumType, value);
            }

            throw new FormatException(Environment.GetResourceString("Format_InvalidEnumFormatSpecification"));
        }

Usage Example

        /// <summary>
        /// 枚举类型转换List<SelectListItem>
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static List <SelectListItem> EnumConvertToSelectListItem(this Type type)
        {
            List <SelectListItem> list = new List <SelectListItem>();

            if (type.IsEnum)
            {
                string[] names = Enum.GetNames(type);
                foreach (string name in names)
                {
                    list.Add(new SelectListItem {
                        Text = name, Value = Enum.Format(type, Enum.Parse(type, name), "d"), Selected = false
                    });
                }
            }
            return(list);
        }
All Usage Examples Of System.Enum::Format