Пример #1
0
        /// <summary>
        /// Throws a System.ComponentModel.InvalidEnumArgumentException with the specified message.
        /// The parameter name for the exception will be the value of ensureArg.ArgumentName.
        /// </summary>
        /// <typeparam name="TEnum">The type parameter of the IEnsureArg instance.</typeparam>
        /// <param name="ensureArg">The IEnsureArg instance to throw an exception for.</param>
        /// <param name="exceptionMessage">
        /// The message to use in the exception. If no exception message is supplied then
        /// ensureArg.ExceptionMessage will be used.
        /// </param>
        public static void ThrowInvalidEnumArgumentException <TEnum>(
            this IEnsureArg <TEnum> ensureArg,
            string exceptionMessage)
            where TEnum : struct, IComparable, IFormattable // Closest we can get to System.Enum and be CLSCompliant.
        {
            string message = ensureArg.FormatInvalidEnumArgumentException(exceptionMessage);

            if (message == null)
            {
                int enumValue = Convert.ToInt32(ensureArg.Value, CultureInfo.InvariantCulture);
                throw new InvalidEnumArgumentException(ensureArg.ArgumentName, enumValue, ensureArg.Value.GetType());
            }

            throw new InvalidEnumArgumentException(message);
        }