andrew4582

Guard

Sep 7th, 2010
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.34 KB | None | 0 0
  1.  using System;
  2.  
  3. namespace System {
  4.     /// <summary>
  5.     /// Provides utility methods to guard parameter and local variables.
  6.     /// </summary>
  7.     public class Guard {
  8.  
  9.         public static string GetErrorMessage(Exception error) {
  10. #if DEBUG
  11.             return error.ToString();
  12. #else
  13.             return error.Message;
  14. #endif
  15.         }
  16.         /// <summary>
  17.         /// Throws an exception of type <typeparamref name="TException"/> with the specified message
  18.         /// when the assertion statement is true.
  19.         /// </summary>
  20.         /// <typeparam name="TException">The type of exception to throw.</typeparam>
  21.         /// <param name="assertion">The assertion to evaluate. If true then the <typeparamref name="TException"/> exception is thrown.</param>
  22.         /// <param name="message">string. The exception message to throw.</param>
  23.         public static void Against<TException>(bool assertion,string message = null) where TException:Exception {
  24.             if(assertion) {
  25.                 throw (TException)Activator.CreateInstance(typeof(TException),message);
  26.             }
  27.         }
  28.  
  29.         /// <summary>
  30.         /// Throws an exception of type <typeparamref name="TException"/> with the specified message
  31.         /// when the assertion
  32.         /// </summary>
  33.         /// <typeparam name="TException"></typeparam>
  34.         /// <param name="assertion"></param>
  35.         /// <param name="message"></param>
  36.         public static void Against<TException>(Func<bool> assertion,string message = null) where TException:Exception {
  37.             //Execute the lambda and if it evaluates to true then throw the exception.
  38.             if(assertion())
  39.                 throw (TException)Activator.CreateInstance(typeof(TException),message);
  40.         }
  41.         public static void AgainstNullException(bool assertion,string message = "Argument is null") {
  42.             Against<ArgumentNullException>(assertion,message);
  43.         }
  44.         public static void AgainstNullException(object objectRefrence,string message = "Argument is null") {
  45.             Against<ArgumentNullException>(objectRefrence == null,message);
  46.         }
  47.  
  48.         /// <summary>
  49.         /// Throws a <see cref="InvalidOperationException"/> when the specified object
  50.         /// instance does not inherit from <typeparamref name="TBase"/> type.
  51.         /// </summary>
  52.         /// <typeparam name="TBase">The base type to check for.</typeparam>
  53.         /// <param name="instance">The object to check if it inherits from <typeparamref name="TBase"/> type.</param>
  54.         /// <param name="message">string. The exception message to throw.</param>
  55.         public static void InheritsFrom<TBase>(object instance,string message) where TBase:Type {
  56.             InheritsFrom<TBase>(instance.GetType(),message);
  57.         }
  58.  
  59.         /// <summary>
  60.         /// Throws a <see cref="InvalidOperationException"/> when the specified type does not
  61.         /// inherit from the <typeparamref name="TBase"/> type.
  62.         /// </summary>
  63.         /// <typeparam name="TBase">The base type to check for.</typeparam>
  64.         /// <param name="type">The <see cref="Type"/> to check if it inherits from <typeparamref name="TBase"/> type.</param>
  65.         /// <param name="message">string. The exception message to throw.</param>
  66.         public static void InheritsFrom<TBase>(Type type,string message) {
  67.             if(type.BaseType != typeof(TBase))
  68.                 throw new InvalidOperationException(message);
  69.         }
  70.  
  71.         /// <summary>
  72.         /// Throws a <see cref="InvalidOperationException"/> when the specified object
  73.         /// instance does not implement the <typeparamref name="TInterface"/> interface.
  74.         /// </summary>
  75.         /// <typeparam name="TInterface">The interface type the object instance should implement.</typeparam>
  76.         /// <param name="instance">The object insance to check if it implements the <typeparamref name="TInterface"/> interface</param>
  77.         /// <param name="message">string. The exception message to throw.</param>
  78.         public static void Implements<TInterface>(object instance,string message) {
  79.             Implements<TInterface>(instance.GetType(),message);
  80.         }
  81.  
  82.         /// <summary>
  83.         /// Throws an <see cref="InvalidOperationException"/> when the specified type does not
  84.         /// implement the <typeparamref name="TInterface"/> interface.
  85.         /// </summary>
  86.         /// <typeparam name="TInterface">The interface type that the <paramref name="type"/> should implement.</typeparam>
  87.         /// <param name="type">The <see cref="Type"/> to check if it implements from <typeparamref name="TInterface"/> interface.</param>
  88.         /// <param name="message">string. The exception message to throw.</param>
  89.         public static void Implements<TInterface>(Type type,string message) {
  90.             if(!typeof(TInterface).IsAssignableFrom(type))
  91.                 throw new InvalidOperationException(message);
  92.         }
  93.  
  94.         /// <summary>
  95.         /// Throws an <see cref="InvalidOperationException"/> when the specified object instance is
  96.         /// not of the specified type.
  97.         /// </summary>
  98.         /// <typeparam name="TType">The Type that the <paramref name="instance"/> is expected to be.</typeparam>
  99.         /// <param name="instance">The object instance whose type is checked.</param>
  100.         /// <param name="message">The message of the <see cref="InvalidOperationException"/> exception.</param>
  101.         public static void TypeOf<TType>(object instance,string message) {
  102.             if(!(instance is TType))
  103.                 throw new InvalidOperationException(message);
  104.         }
  105.  
  106.         /// <summary>
  107.         /// Throws an exception if an instance of an object is not equal to another object instance.
  108.         /// </summary>
  109.         /// <typeparam name="TException">The type of exception to throw when the guard check evaluates false.</typeparam>
  110.         /// <param name="compare">The comparison object.</param>
  111.         /// <param name="instance">The object instance to compare with.</param>
  112.         /// <param name="message">string. The message of the exception.</param>
  113.         public static void IsEqual<TException>(object compare,object instance,string message) where TException:Exception {
  114.             if(compare != instance)
  115.                 throw (TException)Activator.CreateInstance(typeof(TException),message);
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment