Skip to content

Instantly share code, notes, and snippets.

@SteveDunn
Created February 7, 2012 12:48
Show Gist options
  • Save SteveDunn/1759542 to your computer and use it in GitHub Desktop.
Save SteveDunn/1759542 to your computer and use it in GitHub Desktop.
A utility class to validate parameters (add to your projects as a linked file)
using System;
using System.Collections ;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis ;
using System.Linq ;
using JetBrains.Annotations ;
// PLEASE DON'T MAKE ANYTHING PUBLIC IN THIS FILE. THIS FILE (AND JETBRAINS.ANNOTATIONS) IS INTENDED
// TO BE INCLUDED AS A LINKED FILE INTO WHATEVER PROJECTS NEEDS THEM.
internal class CoverageExcludeAttribute : Attribute
{
}
/// <summary>
/// Guard class, used for guard clauses and argument validation.
/// <para>
/// Don't put this in a namespace or an assembly, simply add it your project as a linked file.
/// This is to stop ambiguous instances where 2 or more assemblies contains this type.
/// </para>
/// </summary>
[CoverageExclude]
[DebuggerNonUserCode] // (see http://blog.functionalfun.net/2008/05/debuggernonusercode-suppressing.html)
static class Guard
{
[DebuggerHidden]
[AssertionMethod]
[PublicAPI]
public static void ArgumentNotNull< T >(
[NotNull, InvokerParameterName] string argName,
[AssertionCondition( AssertionConditionType.IS_NOT_NULL ), NoEnumeration] T argValue ) where T : class
{
if( argValue == null )
{
throw new ArgumentNullException( argName ) ;
}
}
[DebuggerHidden]
[SuppressMessage(
"Microsoft.Usage",
"CA2208:InstantiateArgumentExceptionsCorrectly",
Justification = @"Cannot call the overload that takes a parameter name as I'm not passed a parameter name." )]
[AssertionMethod]
[PublicAPI]
public static void ArgumentNotNull< T >(
[AssertionCondition( AssertionConditionType.IS_NOT_NULL ), NoEnumeration] T value ) where T : class
{
if( value == null )
{
throw new ArgumentNullException( ) ;
}
}
[DebuggerHidden]
[AssertionMethod]
[PublicAPI]
public static void GenericArgumentNotNull<T>(
[NotNull, InvokerParameterName] string argName,
[AssertionCondition(AssertionConditionType.IS_NOT_NULL), NoEnumeration] T arg)
{
Type type = typeof(T);
if (!type.IsValueType
|| (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Nullable<>))))
{
ArgumentNotNull(argName, (object)arg);
}
}
[DebuggerHidden]
[AssertionMethod]
[PublicAPI]
public static void ArgumentsNotNull(
[NotNull, InvokerParameterName] string argName,
[AssertionCondition(AssertionConditionType.IS_NOT_NULL), InstantHandle] IEnumerable argValue )
{
// ReSharper disable PossibleMultipleEnumeration
ArgumentNotNull( argName, argValue ) ;
// ReSharper restore PossibleMultipleEnumeration
if( argValue.Cast<object>( ).Any( obj => obj == null ) )
{
throw new ArgumentException( @"Argument cannot contain a null value.", argName ) ;
}
}
[DebuggerHidden]
[AssertionMethod]
[PublicAPI]
public static void StringArgumentNotEmpty(
[NotNull, InvokerParameterName] string argName,
[AssertionCondition(AssertionConditionType.IS_NOT_NULL)] string argValue )
{
ArgumentNotNull( argName, argValue ) ;
if( argValue.Length == 0 )
{
throw new ArgumentException( @"Argument cannot be zero length.", argName ) ;
}
}
[DebuggerHidden]
[AssertionMethod]
[PublicAPI]
public static void StringArgumentNotNullOrEmpty(
[NotNull, InvokerParameterName] string argName,
[AssertionCondition(AssertionConditionType.IS_NOT_NULL)] string argValue )
{
ArgumentNotNull( argName, argValue ) ;
StringArgumentNotEmpty( argName, argValue ) ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment