示例#1
0
        /// <summary>
        /// Guards against an argument being null or less than the specified minimum or greater than the specified
        /// maximum. Will throw an <see cref="ArgumentNullException">ArgumentNullException</see> if the argument is null. Will
        /// throw an <see cref="ArgumentOutOfRangeException">ArgumentOutOfRangeException</see> if the argument is less than the
        /// specified minimum or greater than the specified maximum.
        /// </summary>
        /// <typeparam name="T">A reference type.</typeparam>
        /// <param name="argumentValue">The argument value to guard.</param>
        /// <param name="minimumAllowedValue">The minimum allowed value.</param>
        /// <param name="maximumAllowedValue">The maximum allowed value.</param>
        /// <param name="argumentName">Name of the argument. Can be optionally specified to be included in the raised exception.</param>
        /// <param name="exceptionMessage">
        /// (Optional) Custom error message. A specific error message that can be used to describe
        /// the exception in more detail than the default message.
        /// </param>
        /// <param name="additionalData">(Optional) Additional information to add to the Data property of the thrown exception.</param>
        /// <exception cref="ArgumentNullException">Will be thrown when <c>argumentValue</c> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// Will be thrown when <c>argumentValue</c> is less than the specified
        /// minimum or greater than the specified maximum.
        /// </exception>
        /// <example>
        ///     <code>
        /// public void MyAmazingMethod(string myArgument, DateTime dob)
        /// {
        ///     GuardAgainst.ArgumentBeingNullOrOutOfRange(myArgument, "A", "Z", nameof(myArgument));
        ///     GuardAgainst.ArgumentBeingNullOrOutOfRange(dob, yearTwoThousand, DateTime.Now, nameof(dob));
        ///
        ///     // Remaining code omitted.
        /// }
        /// </code>
        /// </example>
        public static void ArgumentBeingNullOrOutOfRange <T>(T argumentValue,
                                                             T minimumAllowedValue,
                                                             T maximumAllowedValue,
                                                             string argumentName     = null,
                                                             string exceptionMessage = null,
                                                             IDictionary <object, object> additionalData = default)
            where T : class, IComparable <T>
        {
            if (ReferenceEquals(argumentValue, null))
            {
                var ex = new ArgumentNullException(argumentName.ToNullIfWhitespace(),
                                                   exceptionMessage.ToNullIfWhitespace());

                ex.AddData(additionalData);
                throw ex;
            }

            if (!argumentValue.IsInRange(minimumAllowedValue, maximumAllowedValue))
            {
                var ex = new ArgumentOutOfRangeException(argumentName.ToNullIfWhitespace(), argumentValue,
                                                         exceptionMessage.ToNullIfWhitespace());

                ex.AddData(additionalData);
                throw ex;
            }
        }
示例#2
0
        /// <summary>
        /// Guards against an argument being greater than the specified maximum. Will throw an
        /// <see cref="ArgumentOutOfRangeException">ArgumentOutOfRangeException</see> if the argument is greater than the specified
        /// maximum.
        /// </summary>
        /// <typeparam name="T">A reference type.</typeparam>
        /// <param name="argumentValue">The argument value to guard.</param>
        /// <param name="maximumAllowedValue">The maximum allowed value.</param>
        /// <param name="argumentName">Name of the argument. Can be optionally specified to be included in the raised exception.</param>
        /// <param name="exceptionMessage">
        /// (Optional) Custom error message. A specific error message that can be used to describe
        /// the exception in more detail than the default message.
        /// </param>
        /// <param name="additionalData">(Optional) Additional information to add to the Data property of the thrown exception.</param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// Will be thrown when <c>argumentValue</c> is greater than the specified
        /// maximum.
        /// </exception>
        /// <example>
        ///     <code>
        /// public void MyAmazingMethod(int myArgument, DateTime dob)
        /// {
        ///     GuardAgainst.ArgumentBeingGreaterThanMaximum(myArgument, 100, nameof(myArgument));
        ///     GuardAgainst.ArgumentBeingGreaterThanMaximum(dob, DateTime.Now, nameof(dob));
        ///
        ///     // Remaining code omitted.
        /// }
        /// </code>
        /// </example>
        public static void ArgumentBeingGreaterThanMaximum <T>(T argumentValue,
                                                               T maximumAllowedValue,
                                                               string argumentName     = null,
                                                               string exceptionMessage = null,
                                                               IDictionary <object, object> additionalData = default)
            where T : IComparable <T>
        {
            if (ReferenceEquals(argumentValue, null))
            {
                return;
            }

            if (argumentValue.IsMoreThan(maximumAllowedValue))
            {
                var ex = new ArgumentOutOfRangeException(argumentName.ToNullIfWhitespace(), argumentValue,
                                                         exceptionMessage.ToNullIfWhitespace());

                ex.AddData(additionalData);
                throw ex;
            }
        }