Skip to content

Instantly share code, notes, and snippets.

@agross
Created March 10, 2009 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agross/77078 to your computer and use it in GitHub Desktop.
Save agross/77078 to your computer and use it in GitHub Desktop.
using System;
using System.Linq.Expressions;
namespace Indigo.Tools
{
public static class Ensure
{
public static ThrowingValidator Argument(string argument)
{
return new ThrowingValidator(argument);
}
public static ThrowingValidator Property<T>(Expression<Func<T, object>> property)
{
MemberExpression memberExpression = null;
if (property.Body.NodeType == ExpressionType.Convert)
{
UnaryExpression body = (UnaryExpression)property.Body;
memberExpression = body.Operand as MemberExpression;
}
else if (property.Body.NodeType == ExpressionType.MemberAccess)
{
memberExpression = property.Body as MemberExpression;
}
if (memberExpression == null)
{
throw new ArgumentException("Not a member access", "property");
}
return new ThrowingValidator(memberExpression.Member.Name);
}
}
public class ThrowingValidator
{
readonly string _argumentOrPropertyName;
bool _assertion = true;
internal ThrowingValidator(string argumentOrPropertyName)
{
_argumentOrPropertyName = argumentOrPropertyName;
}
public ThrowHelper Satisfies(bool assertion)
{
return new ThrowHelper(_argumentOrPropertyName, assertion);
}
/// <summary>
/// Helper method which throws an <see cref="ArgumentNullException"/> if the specified value is <c>null</c>.
/// </summary>
/// <typeparam name="T">The type of the argument.</typeparam>
/// <param name="value">The value to check.</param>
/// <exception cref="T:System.ArgumentNullException">
/// Thrown when <paramref name="value"/> is <c>null</c>.
/// </exception>
public void IsNotNull<T>(T value)
{
_assertion = value != null;
With<ArgumentNullException>(null);
}
/// <summary>
/// Helper method which throws an <see cref="ArgumentNullException" /> if the specified value is <c>null</c> or zero.
/// </summary>
/// <typeparam name="T">The type of the argument.</typeparam>
/// <param name="value">The value to check.</param>
/// <exception cref="T:System.ArgumentNullException">
/// Thrown when <paramref name="value"/> is <c>null</c> or zero.
/// </exception>
public void IsNotNullOrDefault<T>(T value)
{
_assertion = !(value == null || Equals(value, default(T)));
With<ArgumentNullException>(null);
}
/// <summary>
/// Helper method which throws an <see cref="ArgumentException"/> if the specified value is uninitialized (meaning holding
/// the default value).
/// </summary>
/// <param name="value">The value.</param>
/// <param name="message">The name of the argument which is checked.</param>
/// <exception cref="T:System.ArgumentException">
/// Thrown when <paramref message="value"/> is <c>null</c> for reference types, or zero for value types.
/// </exception>
public void IsNotDefault<T>(T value, string message)
{
_assertion = !(Equals(value, default(T)) && !typeof(T).IsEnum);
With<ArgumentException>(message);
}
/// <summary>
/// Checks a string argument to ensure it isn't null or empty.
/// </summary>
/// <param name="value">The argument value to check.</param>
/// <param name="message">The name of the argument (if the argument is null) or the message (if the argument string is
/// empty).</param>
/// <exception cref="T:System.ArgumentNullException">
/// Thrown when <paramref message="value"/> is <c>null</c>.
/// </exception>
/// <exception cref="T:System.ArgumentException">
/// Thrown when <paramref message="value"/> contains an empty
/// <see cref="string"/>.
/// </exception>
public void IsNotNullOrEmptyString(string value, string message)
{
_assertion = !String.IsNullOrEmpty(value);
With<ArgumentNullException>(message);
}
void With<TExceptionToThrow>(string message) where TExceptionToThrow : Exception
{
new ThrowHelper(_argumentOrPropertyName, _assertion).With<TExceptionToThrow>(message);
}
#region Nested type: ThrowHelper
public class ThrowHelper
{
readonly string _argumentOrPropertyName;
readonly bool _assertion;
internal ThrowHelper(string argumentOrPropertyName, bool assertion)
{
_argumentOrPropertyName = argumentOrPropertyName;
_assertion = assertion;
}
public void With<TExceptionToThrow>(string message) where TExceptionToThrow : Exception
{
ValidatePreCondition<TExceptionToThrow>(message);
}
void ValidatePreCondition<TExceptionToThrow>(string message) where TExceptionToThrow : Exception
{
if (_assertion)
{
return;
}
if (typeof(TExceptionToThrow) == typeof(ArgumentException))
{
throw (TExceptionToThrow) Activator.CreateInstance(typeof(TExceptionToThrow),
message,
_argumentOrPropertyName);
}
if (typeof(TExceptionToThrow) == typeof(ArgumentNullException) ||
typeof(TExceptionToThrow) == typeof(ArgumentOutOfRangeException))
{
throw (TExceptionToThrow) Activator.CreateInstance(typeof(TExceptionToThrow),
_argumentOrPropertyName,
message ?? String.Empty);
}
throw (TExceptionToThrow) Activator.CreateInstance(typeof(TExceptionToThrow), message);
}
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment