/Rhino.Etl.Core/Guard.cs

http://github.com/ayende/rhino-etl · C# · 52 lines · 19 code · 2 blank · 31 comment · 4 complexity · d4b790e9132b2dcafe4ca94d1cc62af2 MD5 · raw file

  1. using System;
  2. namespace Rhino.Etl.Core
  3. {
  4. /// <summary>
  5. /// Helper class for guard statements, which allow prettier
  6. /// code for guard clauses
  7. /// </summary>
  8. public class Guard
  9. {
  10. /// <summary>
  11. /// Will throw a <see cref="InvalidOperationException"/> if the assertion
  12. /// is true, with the specificied message.
  13. /// </summary>
  14. /// <param name="assertion">if set to <c>true</c> [assertion].</param>
  15. /// <param name="message">The message.</param>
  16. /// <example>
  17. /// Sample usage:
  18. /// <code>
  19. /// Guard.Against(string.IsNullOrEmpty(name), "Name must have a value");
  20. /// </code>
  21. /// </example>
  22. public static void Against(bool assertion, string message)
  23. {
  24. if (assertion == false)
  25. return;
  26. throw new InvalidOperationException(message);
  27. }
  28. /// <summary>
  29. /// Will throw exception of type <typeparamref name="TException"/>
  30. /// with the specified message if the assertion is true
  31. /// </summary>
  32. /// <typeparam name="TException"></typeparam>
  33. /// <param name="assertion">if set to <c>true</c> [assertion].</param>
  34. /// <param name="message">The message.</param>
  35. /// <example>
  36. /// Sample usage:
  37. /// <code>
  38. /// <![CDATA[
  39. /// Guard.Against<ArgumentException>(string.IsNullOrEmpty(name), "Name must have a value");
  40. /// ]]>
  41. /// </code>
  42. /// </example>
  43. public static void Against<TException>(bool assertion, string message) where TException : Exception
  44. {
  45. if (assertion == false)
  46. return;
  47. throw (TException)Activator.CreateInstance(typeof(TException), message);
  48. }
  49. }
  50. }