Пример #1
0
        /// <summary>
        /// Gets a dictionary of the names and values of an Enum type.
        /// </summary>
        /// <param name="enumType">The enum type to get names and values for.</param>
        /// <returns></returns>
        public static EnumValues <TUnderlyingType> GetNamesAndValues <TUnderlyingType>(Type enumType) where TUnderlyingType : struct
        {
            if (enumType == null)
            {
                throw new ArgumentNullException("enumType");
            }

            ValidationUtils.ArgumentTypeIsEnum(enumType, "enumType");

            IList <object> enumValues = GetValues(enumType);
            IList <string> enumNames  = GetNames(enumType);

            EnumValues <TUnderlyingType> nameValues = new EnumValues <TUnderlyingType>();

            for (int i = 0; i < enumValues.Count; i++)
            {
                try
                {
                    nameValues.Add(new EnumValue <TUnderlyingType>(enumNames[i], (TUnderlyingType)Convert.ChangeType(enumValues[i], typeof(TUnderlyingType), CultureInfo.CurrentCulture)));
                }
                catch (OverflowException e)
                {
                    throw new Exception(
                              string.Format("Value from enum with the underlying type of {0} cannot be added to dictionary with a value type of {1}. Value was too large: {2}",
                                            Enum.GetUnderlyingType(enumType), typeof(TUnderlyingType), Convert.ToUInt64(enumValues[i])), e);
                }
            }

            return(nameValues);
        }
        public static EnumValues <TUnderlyingType> GetNamesAndValues <TUnderlyingType>(Type enumType) where TUnderlyingType : struct
        {
            if (enumType == null)
            {
                throw new ArgumentNullException("enumType");
            }
            ValidationUtils.ArgumentTypeIsEnum(enumType, "enumType");
            IList <object> values = EnumUtils.GetValues(enumType);
            IList <string> names  = EnumUtils.GetNames(enumType);
            EnumValues <TUnderlyingType> enumValues = new EnumValues <TUnderlyingType>();

            for (int i = 0; i < values.get_Count(); i++)
            {
                try
                {
                    enumValues.Add(new EnumValue <TUnderlyingType>(names.get_Item(i), (TUnderlyingType)((object)Convert.ChangeType(values.get_Item(i), typeof(TUnderlyingType), CultureInfo.get_CurrentCulture()))));
                }
                catch (OverflowException ex)
                {
                    throw new Exception(string.Format(CultureInfo.get_InvariantCulture(), "Value from enum with the underlying type of {0} cannot be added to dictionary with a value type of {1}. Value was too large: {2}", new object[]
                    {
                        Enum.GetUnderlyingType(enumType),
                        typeof(TUnderlyingType),
                        Convert.ToUInt64(values.get_Item(i), CultureInfo.get_InvariantCulture())
                    }), ex);
                }
            }
            return(enumValues);
        }
Пример #3
0
        public static EnumValues <TUnderlyingType> GetNamesAndValues <TUnderlyingType>(Type enumType) where TUnderlyingType : struct
        {
            if (enumType == null)
            {
                throw new ArgumentNullException("enumType");
            }
            ValidationUtils.ArgumentTypeIsEnum(enumType, "enumType");
            IList <object> values = EnumUtils.GetValues(enumType);
            IList <string> names  = EnumUtils.GetNames(enumType);
            EnumValues <TUnderlyingType> enumValues = new EnumValues <TUnderlyingType>();

            for (int index = 0; index < values.Count; ++index)
            {
                try
                {
                    enumValues.Add(new EnumValue <TUnderlyingType>(names[index], (TUnderlyingType)Convert.ChangeType(values[index], typeof(TUnderlyingType), (IFormatProvider)CultureInfo.CurrentCulture)));
                }
                catch (OverflowException ex)
                {
                    throw new InvalidOperationException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "Value from enum with the underlying type of {0} cannot be added to dictionary with a value type of {1}. Value was too large: {2}", (object)Enum.GetUnderlyingType(enumType), (object)typeof(TUnderlyingType), (object)Convert.ToUInt64(values[index], (IFormatProvider)CultureInfo.InvariantCulture)), (Exception)ex);
                }
            }
            return(enumValues);
        }
Пример #4
0
        public static bool TryParse <T>(string enumMemberName, bool ignoreCase, out T value) where T : struct
        {
            ValidationUtils.ArgumentTypeIsEnum(typeof(T), "T");

            return(MiscellaneousUtils.TryAction(() => Parse <T>(enumMemberName, ignoreCase), out value));
        }
Пример #5
0
        /// <summary>
        /// Parses the specified enum member name, returning it's value.
        /// </summary>
        /// <param name="enumMemberName">Name of the enum member.</param>
        /// <param name="ignoreCase">If set to <c>true</c> ignore case.</param>
        /// <returns></returns>
        public static T Parse <T>(string enumMemberName, bool ignoreCase) where T : struct
        {
            ValidationUtils.ArgumentTypeIsEnum(typeof(T), "T");

            return((T)Enum.Parse(typeof(T), enumMemberName, ignoreCase));
        }