-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEnsureExtensions.cs
More file actions
162 lines (145 loc) · 10.8 KB
/
EnsureExtensions.cs
File metadata and controls
162 lines (145 loc) · 10.8 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Platform.Exceptions.ExtensionRoots;
#pragma warning disable IDE0060 // Remove unused parameter
namespace Platform.Exceptions
{
/// <summary>
/// <para>Provides a set of extension methods for <see cref="EnsureAlwaysExtensionRoot"/> and <see cref="EnsureOnDebugExtensionRoot"/> objects.</para>
/// </summary>
public static class EnsureExtensions
{
#region Always
/// <summary>
/// <para>Ensures that argument is not null. This check is performed regardless of the build configuration.</para>
/// /// </summary>
/// <typeparam name="TArgument"><para>Type of argument.</para></typeparam>
/// <param name="root"><para>The extension root to which this method is bound.</para></param>
/// <param name="argument"><para>The argument.</para></param>
/// <param name="argumentName"><para>The argument's name.</para></param>
/// <param name="message"><para>The message of the thrown exception.</para></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ArgumentNotNull<TArgument>(this EnsureAlwaysExtensionRoot root, TArgument argument, string argumentName, string message)
where TArgument : class
{
if (argument == null)
{
throw new ArgumentNullException(argumentName, message);
}
}
/// <summary>
/// <para>Ensures that argument is not null. This check is performed regardless of the build configuration.</para>
/// /// </summary>
/// <typeparam name="TArgument"><para>Type of argument.</para></typeparam>
/// <param name="root"><para>The extension root to which this method is bound.</para></param>
/// <param name="argument"><para>The argument.</para></param>
/// <param name="argumentName"><para>The argument's name.</para></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ArgumentNotNull<TArgument>(this EnsureAlwaysExtensionRoot root, TArgument argument, string argumentName) where TArgument : class => ArgumentNotNull(root, argument, argumentName, $"Argument {argumentName} is null.");
/// <summary>
/// <para>Ensures that argument is not null. This check is performed regardless of the build configuration.</para>
/// /// </summary>
/// <typeparam name="TArgument"><para>Type of argument.</para></typeparam>
/// <param name="root"><para>The extension root to which this method is bound.</para></param>
/// <param name="argument"><para>The argument.</para></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ArgumentNotNull<TArgument>(this EnsureAlwaysExtensionRoot root, TArgument argument) where TArgument : class => ArgumentNotNull(root, argument, null);
/// <summary>
/// <para>Ensures that the argument meets the criteria. This check is performed regardless of the build configuration.</para>
/// /// </summary>
/// <typeparam name="TArgument"><para>Type of argument.</para></typeparam>
/// <param name="root"><para>The extension root to which this method is bound.</para></param>
/// <param name="argument"><para>The argument.</para></param>
/// <param name="predicate"><para>A predicate that determines whether the argument meets a criterion.</para></param>
/// <param name="argumentName"><para>The argument's name.</para></param>
/// <param name="message"><para>The message of the thrown exception.</para></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ArgumentMeetsCriteria<TArgument>(this EnsureAlwaysExtensionRoot root, TArgument argument, Predicate<TArgument> predicate, string argumentName, string message)
{
if (!predicate(argument))
{
throw new ArgumentException(message, argumentName);
}
}
/// <summary>
/// <para>Ensures that the argument meets the criteria. This check is performed regardless of the build configuration.</para>
/// /// </summary>
/// <typeparam name="TArgument"><para>Type of argument.</para></typeparam>
/// <param name="root"><para>The extension root to which this method is bound.</para></param>
/// <param name="argument"><para>The argument.</para></param>
/// <param name="predicate"><para>A predicate that determines whether the argument meets a criterion.</para></param>
/// <param name="argumentName"><para>The argument's name.</para></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ArgumentMeetsCriteria<TArgument>(this EnsureAlwaysExtensionRoot root, TArgument argument, Predicate<TArgument> predicate, string argumentName) => ArgumentMeetsCriteria(root, argument, predicate, argumentName, $"Argument {argumentName} does not meet the criteria.");
/// <summary>
/// <para>Ensures that the argument meets the criteria. This check is performed regardless of the build configuration.</para>
/// /// </summary>
/// <typeparam name="TArgument"><para>Type of argument.</para></typeparam>
/// <param name="root"><para>The extension root to which this method is bound.</para></param>
/// <param name="argument"><para>The argument.</para></param>
/// <param name="predicate"><para>A predicate that determines whether the argument meets a criterion.</para></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ArgumentMeetsCriteria<TArgument>(this EnsureAlwaysExtensionRoot root, TArgument argument, Predicate<TArgument> predicate) => ArgumentMeetsCriteria(root, argument, predicate, null);
#endregion
#region OnDebug
/// <summary>
/// <para>Ensures that argument is not null. This check is performed only for DEBUG build configuration.</para>
/// /// </summary>
/// <typeparam name="TArgument"><para>Type of argument.</para></typeparam>
/// <param name="root"><para>The extension root to which this method is bound.</para></param>
/// <param name="argument"><para>The argument.</para></param>
/// <param name="argumentName"><para>The argument's name.</para></param>
/// <param name="message"><para>The message of the thrown exception.</para></param>
[Conditional("DEBUG")]
public static void ArgumentNotNull<TArgument>(this EnsureOnDebugExtensionRoot root, TArgument argument, string argumentName, string message) where TArgument : class => Ensure.Always.ArgumentNotNull(argument, argumentName, message);
/// <summary>
/// <para>Ensures that argument is not null. This check is performed only for DEBUG build configuration.</para>
/// /// </summary>
/// <typeparam name="TArgument"><para>Type of argument.</para></typeparam>
/// <param name="root"><para>The extension root to which this method is bound.</para></param>
/// <param name="argument"><para>The argument.</para></param>
/// <param name="argumentName"><para>The argument's name.</para></param>
[Conditional("DEBUG")]
public static void ArgumentNotNull<TArgument>(this EnsureOnDebugExtensionRoot root, TArgument argument, string argumentName) where TArgument : class => Ensure.Always.ArgumentNotNull(argument, argumentName);
/// <summary>
/// <para>Ensures that argument is not null. This check is performed only for DEBUG build configuration.</para>
/// /// </summary>
/// <typeparam name="TArgument"><para>Type of argument.</para></typeparam>
/// <param name="root"><para>The extension root to which this method is bound.</para></param>
/// <param name="argument"><para>The argument.</para></param>
[Conditional("DEBUG")]
public static void ArgumentNotNull<TArgument>(this EnsureOnDebugExtensionRoot root, TArgument argument) where TArgument : class => Ensure.Always.ArgumentNotNull(argument);
/// <summary>
/// <para>Ensures that the argument meets the criteria. This check is performed only for DEBUG build configuration.</para>
/// /// </summary>
/// <typeparam name="TArgument"><para>Type of argument.</para></typeparam>
/// <param name="root"><para>The extension root to which this method is bound.</para></param>
/// <param name="argument"><para>The argument.</para></param>
/// <param name="predicate"><para>A predicate that determines whether the argument meets a criterion.</para></param>
/// <param name="argumentName"><para>The argument's name.</para></param>
/// <param name="message"><para>The message of the thrown exception.</para></param>
[Conditional("DEBUG")]
public static void ArgumentMeetsCriteria<TArgument>(this EnsureOnDebugExtensionRoot root, TArgument argument, Predicate<TArgument> predicate, string argumentName, string message) => Ensure.Always.ArgumentMeetsCriteria(argument, predicate, argumentName, message);
/// <summary>
/// <para>Ensures that the argument meets the criteria. This check is performed only for DEBUG build configuration.</para>
/// /// </summary>
/// <typeparam name="TArgument"><para>Type of argument.</para></typeparam>
/// <param name="root"><para>The extension root to which this method is bound.</para></param>
/// <param name="argument"><para>The argument.</para></param>
/// <param name="predicate"><para>A predicate that determines whether the argument meets a criterion.</para></param>
/// <param name="argumentName"><para>The argument's name.</para></param>
[Conditional("DEBUG")]
public static void ArgumentMeetsCriteria<TArgument>(this EnsureOnDebugExtensionRoot root, TArgument argument, Predicate<TArgument> predicate, string argumentName) => Ensure.Always.ArgumentMeetsCriteria(argument, predicate, argumentName);
/// <summary>
/// <para>Ensures that the argument meets the criteria. This check is performed only for DEBUG build configuration.</para>
/// /// </summary>
/// <typeparam name="TArgument"><para>Type of argument.</para></typeparam>
/// <param name="root"><para>The extension root to which this method is bound.</para></param>
/// <param name="argument"><para>The argument.</para></param>
/// <param name="predicate"><para>A predicate that determines whether the argument meets a criterion.</para></param>
[Conditional("DEBUG")]
public static void ArgumentMeetsCriteria<TArgument>(this EnsureOnDebugExtensionRoot root, TArgument argument, Predicate<TArgument> predicate) => Ensure.Always.ArgumentMeetsCriteria(argument, predicate);
#endregion
}
}