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

Parse() приватный Метод

private Parse ( Type enumType, String value, bool ignoreCase ) : Object
enumType Type
value String
ignoreCase bool
Результат Object
        public static Object Parse(Type enumType, String value, bool ignoreCase)
        {
            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");

            value = value.Trim();
            if (value.Length == 0)
                throw new ArgumentException(Environment.GetResourceString("Arg_MustContainEnumInfo"));

            // We have 2 code paths here. One if they are values else if they are Strings.
            // values will have the first character as as number or a sign.
            ulong result = 0;

            if (Char.IsDigit(value[0]) || value[0] == '-' || value[0] == '+')
            {
                Type underlyingType = GetUnderlyingType(enumType);
                Object temp;

                try
                {
                    temp = Convert.ChangeType(value, underlyingType, CultureInfo.InvariantCulture);
                    return ToObject(enumType, temp);
                }
                catch (FormatException)
                { // We need to Parse this a String instead. There are cases
                    // when you tlbimp enums that can have values of the form "3D".
                    // Don't fix this code.
                }
            }

            String[] values = value.Split(enumSeperatorCharArray);

            // Find the field.Lets assume that these are always static classes because the class is
            //  an enum.
            HashEntry hashEntry = GetHashEntry(enumType);
            String[] names = hashEntry.names;

            for (int i = 0; i < values.Length; i++)
            {
                values[i] = values[i].Trim(); // We need to remove whitespace characters

                bool success = false;

                for (int j = 0; j < names.Length; j++)
                {
                    if (ignoreCase)
                    {
                        if (String.Compare(names[j], values[i], StringComparison.OrdinalIgnoreCase) != 0)
                            continue;
                    }
                    else
                    {
                        if (!names[j].Equals(values[i]))
                            continue;
                    }

                    ulong item = hashEntry.values[j];

                    result |= item;
                    success = true;
                    break;
                }

                if (!success)

                    // Not found, throw an argument exception.
                    throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Arg_EnumValueNotFound"), value));
            }

            return ToObject(enumType, result);
        }

Same methods

Enum::Parse ( Type enumType, String value ) : Object

Usage Example

        public static List <IEnumValue> GetEnumValues(Type type)
        {
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (type.IsEnum)
            {
                List <IEnumValue> Values = new List <IEnumValue>();

                foreach (string Name in Enum.GetNames(type))
                {
                    Values.Add(
                        new EnumValue()
                    {
                        Value = Convert.ChangeType(
                            Enum.Parse(type, Name),
                            Enum.GetUnderlyingType(type)
                            ).ToString(),
                        Label = Name
                    });
                }

                return(Values);
            }
            else
            {
                return(null);
            }
        }
All Usage Examples Of System.Enum::Parse