PageRenderTime 366ms CodeModel.GetById 181ms app.highlight 2ms RepoModel.GetById 182ms app.codeStats 0ms

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