예제 #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 IList <EnumValue <TUnderlyingType> > GetNamesAndValues <TUnderlyingType>(
            Type enumType)
            where TUnderlyingType : struct
        {
            if (enumType == null)
            {
                throw new ArgumentNullException(nameof(enumType));
            }
            if (!enumType.IsEnum())
            {
                throw new ArgumentException("Type {0} is not an Enum.".FormatWith((IFormatProvider)CultureInfo.InvariantCulture, (object)enumType), nameof(enumType));
            }
            IList <object> values = EnumUtils.GetValues(enumType);
            IList <string> names  = EnumUtils.GetNames(enumType);
            IList <EnumValue <TUnderlyingType> > enumValueList = (IList <EnumValue <TUnderlyingType> >) new List <EnumValue <TUnderlyingType> >();

            for (int index = 0; index < values.Count; ++index)
            {
                try
                {
                    enumValueList.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(enumValueList);
        }
예제 #2
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 i = 0; i < values.Count; i++)
            {
                try
                {
                    enumValues.Add(new EnumValue <TUnderlyingType>(names[i], (TUnderlyingType)((object)Convert.ChangeType(values[i], typeof(TUnderlyingType), CultureInfo.CurrentCulture))));
                }
                catch (OverflowException innerException)
                {
                    throw new Exception(string.Format(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}", new object[]
                    {
                        Enum.GetUnderlyingType(enumType),
                        typeof(TUnderlyingType),
                        Convert.ToUInt64(values[i], CultureInfo.InvariantCulture)
                    }), innerException);
                }
            }
            return(enumValues);
        }
예제 #3
0
        public static IList <EnumValue <TUnderlyingType> > GetNamesAndValues <TUnderlyingType>(Type enumType)
            where TUnderlyingType : struct
        {
            if (enumType == null)
            {
                throw new ArgumentNullException("enumType");
            }
            if (!enumType.IsEnum())
            {
                throw new ArgumentException("Type {0} is not an Enum.".FormatWith(CultureInfo.InvariantCulture, enumType), "enumType");
            }
            IList <object> values = EnumUtils.GetValues(enumType);
            IList <string> names  = EnumUtils.GetNames(enumType);
            IList <EnumValue <TUnderlyingType> > enumValues = new List <EnumValue <TUnderlyingType> >();

            for (int i = 0; i < values.Count; i++)
            {
                try
                {
                    enumValues.Add(new EnumValue <TUnderlyingType>(names[i], (TUnderlyingType)Convert.ChangeType(values[i], typeof(TUnderlyingType), CultureInfo.CurrentCulture)));
                }
                catch (OverflowException overflowException1)
                {
                    OverflowException overflowException = overflowException1;
                    throw new InvalidOperationException(string.Format(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}", new object[] { Enum.GetUnderlyingType(enumType), typeof(TUnderlyingType), Convert.ToUInt64(values[i], CultureInfo.InvariantCulture) }), overflowException);
                }
            }
            return(enumValues);
        }
예제 #4
0
 public static IList <string> GetNames <T>()
 {
     return(EnumUtils.GetNames(typeof(T)));
 }