/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
- using System;
- namespace Rhino.Etl.Core
- {
- /// <summary>
- /// Helper class for guard statements, which allow prettier
- /// code for guard clauses
- /// </summary>
- public class Guard
- {
- /// <summary>
- /// Will throw a <see cref="InvalidOperationException"/> if the assertion
- /// is true, with the specificied message.
- /// </summary>
- /// <param name="assertion">if set to <c>true</c> [assertion].</param>
- /// <param name="message">The message.</param>
- /// <example>
- /// Sample usage:
- /// <code>
- /// Guard.Against(string.IsNullOrEmpty(name), "Name must have a value");
- /// </code>
- /// </example>
- public static void Against(bool assertion, string message)
- {
- if (assertion == false)
- return;
- throw new InvalidOperationException(message);
- }
- /// <summary>
- /// Will throw exception of type <typeparamref name="TException"/>
- /// with the specified message if the assertion is true
- /// </summary>
- /// <typeparam name="TException"></typeparam>
- /// <param name="assertion">if set to <c>true</c> [assertion].</param>
- /// <param name="message">The message.</param>
- /// <example>
- /// Sample usage:
- /// <code>
- /// <![CDATA[
- /// Guard.Against<ArgumentException>(string.IsNullOrEmpty(name), "Name must have a value");
- /// ]]>
- /// </code>
- /// </example>
- public static void Against<TException>(bool assertion, string message) where TException : Exception
- {
- if (assertion == false)
- return;
- throw (TException)Activator.CreateInstance(typeof(TException), message);
- }
- }
- }