-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathThrow.cs
43 lines (39 loc) · 1.25 KB
/
Throw.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
namespace SetToolsVersion
{
using System;
public static class Throw
{
/// <summary>
/// Throws an <see cref="ArgumentNullException"/> if the given value is null
/// </summary>
public static void IfNull<T>(T value, string parameterName)
{
Throw<ArgumentNullException>.If(value == null, parameterName);
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> if the given condition is true
/// </summary>
public static void If(bool condition, string parameterName)
{
Throw<ArgumentException>.If(condition, parameterName);
}
}
public static class Throw<TException>
where TException : Exception
{
/// <summary>
/// Throws an exception of type <see cref="TException"/> if the condition is true
/// </summary>
public static void If(bool condition, string message)
{
if (condition)
{
throw Create(message);
}
}
private static TException Create(string message)
{
return (TException)Activator.CreateInstance(typeof(TException), message);
}
}
}