Created
July 13, 2016 13:46
-
-
Save RAGNOARAKNOS/25a6707d4b4d9d96a4fa21b34605de02 to your computer and use it in GitHub Desktop.
Simple version of Code Contracts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
/// <summary> | |
/// Provides conditional contract assertions. | |
/// </summary> | |
public static class Contract | |
{ | |
#region NotNull | |
/// <summary> | |
/// If 'value' is null, throws an ArgumentNullException. | |
/// </summary> | |
/// <param name="value">Value that must not be null.</param> | |
public static void NotNull(object value) | |
{ | |
Contract.Requires<ArgumentNullException>(value != null); | |
} | |
/// <summary> | |
/// If 'value' is null, throws an ArgumentNullException. | |
/// </summary> | |
/// <param name="value">value that must not be null.</param> | |
/// <param name="paramName">name of the parameter that holds 'value'.</param> | |
public static void NotNull(object value, string paramName) | |
{ | |
Contract.Requires<ArgumentNullException>(value != null, paramName); | |
} | |
/// <summary> | |
/// If 'value' is null, throws an exception of the given type. | |
/// </summary> | |
/// <typeparam name="TException">The type of the exception to throw.</typeparam> | |
/// <param name="value">The value that must not be null.</param> | |
public static void NotNull<TException>(object value) | |
where TException : Exception, new() | |
{ | |
Contract.Requires<TException>(value != null); | |
} | |
/// <summary> | |
/// If 'value' is null, throws an exception of the given type. | |
/// </summary> | |
/// <typeparam name="TException">The type of the exception to throw.</typeparam> | |
/// <param name="value">The value that must not be null.</param> | |
/// <param name="message">A message included if the exception is thrown.</param> | |
public static void NotNull<TException>(object value, string message) | |
where TException : Exception, new() | |
{ | |
Contract.Requires<TException>(value != null, message); | |
} | |
#endregion NotNull | |
#region Null | |
/// <summary> | |
/// If 'value' is not null, throws an InvalidOperationException. | |
/// </summary> | |
/// <param name="value">Value that must be null.</param> | |
public static void Null(object value) | |
{ | |
Contract.Requires<InvalidOperationException>(value == null); | |
} | |
/// <summary> | |
/// If 'value' is not null, throws an InvalidOperationException. | |
/// </summary> | |
/// <param name="value">value that must not be null.</param> | |
/// <param name="message">A message included if the exception is thrown.</param> | |
public static void Null(object value, string message) | |
{ | |
Contract.Requires<InvalidOperationException>(value == null, message); | |
} | |
/// <summary> | |
/// If 'value' is not null, throws an exception of the given type. | |
/// </summary> | |
/// <typeparam name="TException">The type of the exception to throw.</typeparam> | |
/// <param name="value">The value that must not be null.</param> | |
public static void Null<TException>(object value) | |
where TException : Exception, new() | |
{ | |
Contract.Requires<TException>(value == null); | |
} | |
/// <summary> | |
/// If 'value' is not null, throws an exception of the given type. | |
/// </summary> | |
/// <typeparam name="TException">The type of the exception to throw.</typeparam> | |
/// <param name="value">The value that must not be null.</param> | |
/// <param name="message">A message included if the exception is thrown.</param> | |
public static void Null<TException>(object value, string message) | |
where TException : Exception, new() | |
{ | |
Contract.Requires<TException>(value == null, message); | |
} | |
#endregion Null | |
#region Requires | |
/// <summary> | |
/// If 'predicate' is false, throws an exception of the given type. | |
/// </summary> | |
/// <typeparam name="TException">The type of the exception to throw.</typeparam> | |
/// <param name="predicate">The bool that must be true.</param> | |
public static void Requires<TException>(bool predicate) | |
where TException : Exception, new() | |
{ | |
if (!predicate) | |
{ | |
throw new TException(); | |
} | |
} | |
/// <summary> | |
/// If 'predicate' is false, throws an exception of the given type. | |
/// </summary> | |
/// <typeparam name="TException">The type of the exception to throw.</typeparam> | |
/// <param name="predicate">The bool that must be true.</param> | |
/// <param name="message">A message included if the exception is thrown.</param> | |
public static void Requires<TException>(bool predicate, string message) | |
where TException : Exception, new() | |
{ | |
if (!predicate) | |
{ | |
var cons = typeof(TException).GetConstructor(new Type[] { typeof(string) }); | |
if (cons != null) | |
{ | |
throw cons.Invoke(new object[] { message }) as TException; | |
} | |
else | |
{ | |
Debug.WriteLine(message); | |
throw new TException(); | |
} | |
} | |
} | |
#endregion Requires | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment