Skip to content

Instantly share code, notes, and snippets.

@morbidcamel101
Last active February 9, 2017 14:41
Show Gist options
  • Save morbidcamel101/cab7d0fe18262690e56b70d3230ce271 to your computer and use it in GitHub Desktop.
Save morbidcamel101/cab7d0fe18262690e56b70d3230ce271 to your computer and use it in GitHub Desktop.
Common Guard Clauses
public static class Guard
{
#region ArgumentNotNull
/// <summary>
/// Checks an argument to ensure it isn't null
/// </summary>
/// <param name="argumentValue">The argument value to check.</param>
/// <param name="argumentName">The name of the argument.</param>
public static void ArgumentNotNull(object argumentValue, string argumentName)
{
ArgumentNotNull(argumentValue, argumentName, string.Empty);
}
/// <summary>
/// Checks an argument to ensure it isn't null
/// </summary>
/// <param name="argumentValue">The argument value to check.</param>
/// <param name="argumentName">The name of the argument.</param>
/// <param name="customMessage">The custom message.</param>
/// <exception cref="ArgumentNullException">Argument is null.</exception>
public static void ArgumentNotNull(object argumentValue, string argumentName, string customMessage)
{
if (argumentValue == null)
{
if (string.IsNullOrEmpty(customMessage))
{
throw new ArgumentNullException(argumentName);
}
else
{
throw new ArgumentNullException(argumentName, customMessage);
}
}
}
#endregion
#region ArgumentNotNullOrEmptyString
/// <summary>
/// Checks a string argument to ensure it isn't null or empty
/// </summary>
/// <param name="argumentValue">The argument value to check.</param>
/// <param name="argumentName">The name of the argument.</param>
/// <exception cref="ArgumentException"><c>ArgumentException</c>.</exception>
public static void ArgumentNotNullOrEmptyString(string argumentValue, string argumentName)
{
ArgumentNotNull(argumentValue, argumentName);
if (argumentValue.Trim().Length == 0)
throw new ArgumentException("The argument cannot be null or empty string", argumentName);
}
#endregion
#region ArgumentNotNullOrZeroLength
/// <summary>
/// Arguments the length of the not null or zero length.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="argumentName">Name of the argument.</param>
/// <exception cref="ArgumentException"><c>ArgumentException</c>.</exception>
public static void ArgumentNotNullOrZeroLength<T>(T[] source, string argumentName)
{
if (source == null || source.Length == 0)
{
throw new ArgumentException(string.Format("The array cannot be empty or null.", argumentName));
}
}
#endregion
#region ArgumentType
/// <summary>
/// Arguments are enumerations.
/// </summary>
/// <param name="argumentValue">The argument value.</param>
/// <param name="argumentName">Name of the argument.</param>
/// <exception cref="ArgumentException"><c>ArgumentException</c>.</exception>
public static void ArgumentTypeIsEnum(System.Type argumentValue, string argumentName)
{
ArgumentNotNull(argumentValue, argumentName);
if (!argumentValue.IsEnum)
throw new ArgumentException(string.Format("The argument is not an enumeration", argumentName));
}
#endregion
#region ArgumentIsOfType
/// <summary>
/// Check to see that the specified argument is of the specified type when the
/// argument is not type safe.
/// </summary>
/// <param name="argumentValue">The argument value.</param>
/// <param name="type">The type.</param>
/// <param name="argumentName">Name of the argument.</param>
/// <exception cref="ArgumentException"><c>ArgumentException</c>.</exception>
public static void ArgumentIsOfType(
object argumentValue,
System.Type type,
string argumentName)
{
ArgumentNotNull(argumentValue, argumentName);
if (!ReflectionUtil.IsOfType(argumentValue.GetType(), type))
{
throw new ArgumentException(string.Format("The argument is not of the right type. {0} -> {1}: {2}", argumentValue, argumentName, type.Name));
}
}
/// <summary>
/// Check if the argument is of the specified type.
/// </summary>
/// <param name="argumentValue">The argument value.</param>
/// <param name="type">The type.</param>
/// <param name="argumentName">Name of the argument.</param>
/// <exception cref="ArgumentException"><c>ArgumentException</c>.</exception>
public static void ArgumentIsOfType(
System.Type argumentValue,
System.Type type,
string argumentName)
{
ArgumentNotNull(argumentValue, argumentName);
if (!argumentValue.IsOfType(type)
&& !argumentValue.ImplementsInterface(type))
{
throw new ArgumentException(string.Format("The value {0} for argument '{1}' is not {2}", argumentValue, argumentName, type.Name));
}
}
#endregion
#region ItemNotFound
/// <summary>
/// The specified item could not found exception.
/// </summary>
/// <param name="itemDescription">The item description.</param>
/// <exception cref="IndexOutOfRangeException"><c>IndexOutOfRangeException</c>.</exception>
public static void ItemNotFound(string itemDescription, object item)
{
string value = item == null ? "null" : item.ToString();
throw new IndexOutOfRangeException(
string.Format("The specified item cannot be found. ", itemDescription, value));
}
#endregion
#region CheckIndex
/// <summary>
/// Checks the index and makes sure it's not -1 else throw an exeception.
/// </summary>
/// <param name="index">The index.</param>
/// <exception cref="IndexOutOfRangeException"><c>IndexOutOfRangeException</c>.</exception>
public static void CheckIndex(int index)
{
if (index == -1)
{
throw new IndexOutOfRangeException();
}
}
#endregion
#region CheckIndices
/// <summary>
/// Checks the indices against the specified array to make sure that they are valid.
/// </summary>
/// <param name="array">The array.</param>
/// <param name="indices">The indices.</param>
/// <exception cref="IndexOutOfRangeException"><c>IndexOutOfRangeException</c>.</exception>
public static void CheckIndices<T>(T[] array, params int[] indices)
{
ArgumentNotNull(array, "array");
for (int i = 0; i < indices.Length; i++)
{
int index = indices[i];
if (index >= array.Length || index < 0)
{
throw new IndexOutOfRangeException();
}
}
}
#endregion
#region ArgumentNotZero
/// <summary>
/// Arguments the not zero.
/// </summary>
/// <param name="argumentValue">The argument value.</param>
/// <param name="argumentName">Name of the argument.</param>
/// <exception cref="ArgumentException"><c>ArgumentException</c>.</exception>
public static void ArgumentNotZero(int argumentValue, string argumentName)
{
if (argumentValue == 0)
{
throw new ArgumentException(
string.Format("The argument cannot be zero.", argumentName),
argumentName);
}
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment