Exemple #1
0
        /// <summary>
        /// Determines if the specified <paramref name="enumType"/> is an Enum
        /// </summary>
        /// <param name="enumType"><see cref="Type"/> to check.</param>
        /// <returns><c>True</c> if the <paramref name="enumType"/> is an enum; otherwise <c>False</c>.</returns>
        private static bool IsEnum(Type enumType)
        {
            if (!EnumHelper.enumRanges.ContainsKey(enumType) && enumType.IsEnum)
            {
                EnumHelper.enumRanges.Add(enumType, EnumRange.Create(enumType));
            }

            return(EnumHelper.enumRanges.ContainsKey(enumType));
        }
Exemple #2
0
        public static TEnum ValueOf <TEnum>(object value)
            where TEnum : struct
        {
            if (!EnumHelper.IsEnum <TEnum>())
            {
                throw new ArgumentException(EnumHelper.InvalidTEnumMessage);
            }

            if (value.IsNull())
            {
                throw new ArgumentNullException("value");
            }

            TEnum result;

            Type valueType = value.GetType();
            Type enumType  = Enum.GetUnderlyingType(typeof(TEnum));

            try
            {
                if (valueType == typeof(string))
                {
                    if (EnumHelper.TryParseAny <TEnum>((string)value, out result))
                    {
                        return(result);
                    }
                }
                else
                {
                    EnumRange range = EnumHelper.enumRanges[enumType];
                    if (range.Converter.CanConvertFrom(valueType))
                    {
                        return((TEnum)range.Converter.ConvertFrom(value));
                    }
                }
                throw new ArgumentException();
            }
            catch (InvalidCastException cex)
            {
                throw new ArgumentOutOfRangeException(string.Format("The value '{0}' is not a valid value of  '{1}'.", value, typeof(TEnum).FullName), cex);
            }
            catch (InvalidOperationException oex)
            {
                throw new ArgumentOutOfRangeException(string.Format("The value '{0}' is not a valid value of  '{1}'.", value, typeof(TEnum).FullName), oex);
            }
            catch (Exception ex)
            {
                throw new ArgumentOutOfRangeException(string.Format("The value '{0}' is not a valid value of  '{1}'.", value, typeof(TEnum).FullName), ex);
            }
        }
Exemple #3
0
        private static bool TryChange <TEnum>(EnumRange range, double value, out TEnum result)
            where TEnum : struct
        {
            result = default(TEnum);
            bool returnValue = false;

            try
            {
                if (range.IsValid(value))
                {
                    result      = (TEnum)range.Converter.ConvertFrom(value);
                    returnValue = true;
                }
            }
            catch (NotSupportedException)
            {
                result = default(TEnum);
            }
            return(returnValue);
        }