Ejemplo n.º 1
0
        /// <summary>Checks whether <paramref name="enumValue"/> is of the enumeration type <typeparamref name="TEnum"/> and defined within this type.</summary>
        /// <remarks>
        /// When successful, the value is returned as a <c>Nullable</c> of the specified type for direct assignment.
        /// </remarks>
        /// <exception cref="ArgumentException"> If <paramref name="enumValue"/> is not of the specified type. </exception>
        /// <exception cref="ArgumentOutOfRangeException"> If <paramref name="enumValue"/> has a numeric value that is not completely defined within its
        /// enumeration type. For flag types, every bit must correspond to at least one enumeration value. </exception>
        public static TEnum?CheckValidEnumValueAndType <TEnum> ([InvokerParameterName] string argumentName, object enumValue)
            where TEnum : struct
        {
            if (enumValue == null)
            {
                return(default(TEnum?));
            }

            if (!(enumValue is TEnum))
            {
                throw CreateArgumentTypeException(argumentName, enumValue.GetType(), typeof(TEnum));
            }

            if (!EnumUtility.IsValidEnumValue(enumValue))
            {
                throw CreateEnumArgumentOutOfRangeException(argumentName, enumValue);
            }

            return((TEnum?)enumValue);
        }
Ejemplo n.º 2
0
        /// <summary>Checks whether <paramref name="enumValue"/> is of the enumeration type <typeparamref name="TEnum"/>, is defined within this
        /// type, and is not a null reference.</summary>
        /// <remarks>
        /// When successful, the value is returned as the specified type for direct assignment.
        /// </remarks>
        /// <exception cref="ArgumentNullException"> If <paramref name="enumValue"/> is a null reference. </exception>
        /// <exception cref="ArgumentException"> If <paramref name="enumValue"/> is not of the specified type. </exception>
        /// <exception cref="ArgumentOutOfRangeException"> If <paramref name="enumValue"/> has a numeric value that is not completely defined within its
        /// enumeration type. For flag types, every bit must correspond to at least one enumeration value. </exception>
        public static TEnum CheckValidEnumValueAndTypeAndNotNull <TEnum> (
            [InvokerParameterName] string argumentName,
            [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] object enumValue)
            where TEnum : struct
        {
            if (enumValue == null)
            {
                throw new ArgumentNullException(argumentName);
            }

            if (!(enumValue is TEnum))
            {
                throw CreateArgumentTypeException(argumentName, enumValue.GetType(), typeof(TEnum));
            }

            if (!EnumUtility.IsValidEnumValue(enumValue))
            {
                throw CreateEnumArgumentOutOfRangeException(argumentName, enumValue);
            }

            return((TEnum)enumValue);
        }
Ejemplo n.º 3
0
        /// <summary> Converts <paramref name="value"/> into an <see cref="Enum"/> value. </summary>
        /// <param name="context"> An <see cref="ITypeDescriptorContext"/> that provides a format context. </param>
        /// <param name="culture"> The <see cref="CultureInfo"/> to use as the current culture. </param>
        /// <param name="value"> The source value. </param>
        /// <returns> An <see cref="Enum"/> value.  </returns>
        /// <exception cref="NotSupportedException"> The conversion could not be performed. </exception>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            // ReSharper disable ConditionIsAlwaysTrueOrFalse
            if (_isNullable && (value == null || (value is string) && string.IsNullOrEmpty((string)value)))
            {
                return(null);
            }

            if (!(value is string))
            {
                if (value != null && _underlyingType == value.GetType())
                {
                    if (!EnumUtility.IsValidEnumValue(UnderlyingEnumType, value))
                    {
                        throw new ArgumentOutOfRangeException(string.Format("The value {0} is not supported for enumeration '{1}'.", value, UnderlyingEnumType.FullName), (Exception)null);
                    }

                    return(Enum.ToObject(UnderlyingEnumType, value));
                }
            }

            return(base.ConvertFrom(context, culture, value));
            // ReSharper restore ConditionIsAlwaysTrueOrFalse
        }