/// <summary> /// Checks whether the value contained in the IEnsureArg instance is less than to the /// specified other value. /// </summary> /// <typeparam name="T">The type of the value contained in the IEnsureArg instance.</typeparam> /// <param name="ensureArg"> /// The IEnsureArg instance, usually created from an Ensure.Arg() call. /// </param> /// <param name="other">The value to compare against.</param> /// <param name="exceptionMessage"> /// Optional exception message to use if the null check fails. This exception message will /// override the message supplied in the Ensure.Arg() call, if any. /// </param> /// <returns>The ensureArg instance.</returns> /// <exception cref="System.ArgumentNullException">ensureArg.Value is null.</exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// ensureArg.Value is greater than or equal to other. /// </exception> public static IEnsureArg <T> IsLessThan <T>( this IEnsureArg <T> ensureArg, T other, string exceptionMessage = null) where T : IComparable <T> { ensureArg.ValidateEnsureArgIsNotNull(); if (!ensureArg.Value.IsLessThan(other)) { ensureArg.ThrowArgumentOutOfRangeException(other, exceptionMessage); } return(ensureArg); }
/// <summary> /// Checks whether the value contained in the IEnsureArg instance is between the specified min /// and max values inclusively. /// </summary> /// <example> /// <para>int a = 2;</para> /// <para>Ensure.Arg(a).IsBetween(1, 3); // Doesn't throw.</para> /// <para>Ensure.Arg(a).IsBetween(2, 3); // Throws ArgumentOutOfRangeException.</para> /// <para>Ensure.Arg(a).IsBetween(1, 2); // Throws ArgumentOutOfRangeException.</para> /// <para>Ensure.Arg(a).IsBetweenOrEqualTo(1, 3); // Doesn't throw.</para> /// <para>Ensure.Arg(a).IsBetweenOrEqualTo(2, 3); // Doesn't throw.</para> /// <para>Ensure.Arg(a).IsBetweenOrEqualTo(1, 2); // Doesn't throw.</para> /// <para>Ensure.Arg(a).IsBetweenOrEqualTo(3, 4); // Throws ArgumentOutOfRangeException.</para> /// </example> /// <typeparam name="T">The type of the value contained in the IEnsureArg instance.</typeparam> /// <param name="ensureArg"> /// The IEnsureArg instance, usually created from an Ensure.Arg() call. /// </param> /// <param name="min">The minimum value.</param> /// <param name="max">The maximum value.</param> /// <param name="exceptionMessage"> /// Optional exception message to use if the null check fails. This exception message will /// override the message supplied in the Ensure.Arg() call, if any. /// </param> /// <returns>The ensureArg instance.</returns> /// <exception cref="System.ArgumentNullException">ensureArg.Value is null.</exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// ensureArg.Value is outside the range of min and max. /// </exception> public static IEnsureArg <T> IsBetweenOrEqualTo <T>( this IEnsureArg <T> ensureArg, T min, T max, string exceptionMessage = null) where T : IComparable <T> { ensureArg.ValidateEnsureArgIsNotNull(); if (!ensureArg.Value.IsBetweenOrEqualTo(min, max)) { ensureArg.ThrowArgumentOutOfRangeException(min, max, exceptionMessage); } return(ensureArg); }
/// <summary> /// Creates an exception message appropriate for an ArgumentException given the /// context of the ensureArg instance. /// </summary> /// <typeparam name="T">The type parameter of the IEnsureArg instance.</typeparam> /// <param name="ensureArg">The IEnsureArg instance to format an exception message for.</param> /// <param name="exceptionMessage">An exception message to use which may provide place holders. /// The available place holders are: {argName} = ensureArg.ArgumentName, {arg} = ensureArg.Value. /// If this parameter is null the ensureArg.Exception message will be used.</param> /// <returns>A formatted exception message or null if no exception message was found.</returns> public static string FormatArgumentExceptionMessage <T>(this IEnsureArg <T> ensureArg, string exceptionMessage) { ensureArg.ValidateEnsureArgIsNotNull(); string message = exceptionMessage ?? ensureArg.ExceptionMessage; if (message != null) { return(formatter.Format( CultureInfo.InvariantCulture, message, new { argName = ensureArg.ArgumentName, arg = ensureArg.Value })); } return(null); }
public static IEnsureArg <TEnum> IsValidEnumValue <TEnum>( this IEnsureArg <TEnum> ensureArg, string exceptionMessage = null) where TEnum : struct, IComparable, IFormattable // Closest we can get to System.Enum and be CLSCompliant. { ensureArg.ValidateEnsureArgIsNotNull(); Type enumType = ensureArg.Value.GetType(); if (!enumType.IsEnum) { throw new ArgumentException("TEnum must be an enumerated type", "TEnum"); } if (!Enum.IsDefined(enumType, ensureArg.Value)) { ensureArg.ThrowInvalidEnumArgumentException(exceptionMessage); } return(ensureArg); }
/// <summary> /// Creates an exception message appropriate for an ArgumentNullException given the /// context of the ensureArg instance. /// </summary> /// <typeparam name="T">The type parameter of the IEnsureArg instance.</typeparam> /// <param name="ensureArg">The IEnsureArg instance to format an exception message for.</param> /// <param name="exceptionMessage">An exception message to use which may provide place holders. /// The available place holders are: {argName} = ensureArg.ArgumentName, {arg} = ensureArg.Value. /// If this parameter is null the ensureArg.Exception message will be used.</param> /// <returns>A formatted exception message or null if no exception message was found.</returns> public static string FormatArgumentNullExceptionMessage <T>(this IEnsureArg <T> ensureArg, string exceptionMessage) { ensureArg.ValidateEnsureArgIsNotNull(); return(ensureArg.FormatArgumentExceptionMessage(exceptionMessage)); }