1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
- using System . Reflection ;
5
4
6
5
namespace EasyNetQ
7
6
{
@@ -26,9 +25,14 @@ internal static class Preconditions
26
25
/// <exception cref="ArgumentException">
27
26
/// Thrown if <paramref name="name"/> is blank.
28
27
/// </exception>
29
- public static void CheckNotNull < T > ( T value , string name )
28
+ public static void CheckNotNull < T > ( T value , string name ) where T : class
30
29
{
31
- CheckNotNull ( value , name , string . Format ( "{0} must not be null" , name ) ) ;
30
+ if ( value == null )
31
+ {
32
+ CheckNotBlank ( name , nameof ( name ) , "name must not be blank" ) ;
33
+
34
+ throw new ArgumentNullException ( name , string . Format ( "{0} must not be null" , name ) ) ;
35
+ }
32
36
}
33
37
34
38
/// <summary>
@@ -52,12 +56,12 @@ public static void CheckNotNull<T>(T value, string name)
52
56
/// Thrown if <paramref name="name"/> or <paramref name="message"/> are
53
57
/// blank.
54
58
/// </exception>
55
- public static void CheckNotNull < T > ( T value , string name , string message )
59
+ public static void CheckNotNull < T > ( T value , string name , string message ) where T : class
56
60
{
57
61
if ( value == null )
58
62
{
59
- CheckNotBlank ( name , " name" , "name must not be blank" ) ;
60
- CheckNotBlank ( message , " message" , "message must not be blank" ) ;
63
+ CheckNotBlank ( name , nameof ( name ) , "name must not be blank" ) ;
64
+ CheckNotBlank ( message , nameof ( message ) , "message must not be blank" ) ;
61
65
62
66
throw new ArgumentNullException ( name , message ) ;
63
67
}
@@ -85,12 +89,12 @@ public static void CheckNotBlank(string value, string name, string message)
85
89
{
86
90
if ( string . IsNullOrWhiteSpace ( name ) )
87
91
{
88
- throw new ArgumentException ( "name must not be blank" , " name" ) ;
92
+ throw new ArgumentException ( "name must not be blank" , nameof ( name ) ) ;
89
93
}
90
94
91
95
if ( string . IsNullOrWhiteSpace ( message ) )
92
96
{
93
- throw new ArgumentException ( "message must not be blank" , " message" ) ;
97
+ throw new ArgumentException ( "message must not be blank" , nameof ( message ) ) ;
94
98
}
95
99
96
100
if ( string . IsNullOrWhiteSpace ( value ) )
@@ -115,7 +119,15 @@ public static void CheckNotBlank(string value, string name, string message)
115
119
/// </exception>
116
120
public static void CheckNotBlank ( string value , string name )
117
121
{
118
- CheckNotBlank ( value , name , string . Format ( "{0} must not be blank" , name ) ) ;
122
+ if ( string . IsNullOrWhiteSpace ( name ) )
123
+ {
124
+ throw new ArgumentException ( "name must not be blank" , nameof ( name ) ) ;
125
+ }
126
+
127
+ if ( string . IsNullOrWhiteSpace ( value ) )
128
+ {
129
+ throw new ArgumentException ( string . Format ( "{0} must not be blank" , name ) , name ) ;
130
+ }
119
131
}
120
132
121
133
/// <summary>
@@ -134,73 +146,72 @@ public static void CheckNotBlank(string value, string name)
134
146
/// is empty, must not be blank.
135
147
/// </param>
136
148
/// <exception cref="ArgumentException">
137
- /// Thrown if <paramref name="collection"/> is empty, or if
138
- /// <paramref name="value"/> or <paramref name="name"/> are blank.
149
+ /// Thrown if <paramref name="collection"/> is empty, or <c>null</c>.
139
150
/// </exception>
140
151
public static void CheckAny < T > ( IEnumerable < T > collection , string name , string message )
141
152
{
142
153
if ( collection == null || ! collection . Any ( ) )
143
154
{
144
- CheckNotBlank ( name , " name" , "name must not be blank" ) ;
145
- CheckNotBlank ( message , " message" , "message must not be blank" ) ;
155
+ CheckNotBlank ( name , nameof ( name ) , "name must not be blank" ) ;
156
+ CheckNotBlank ( message , nameof ( message ) , "message must not be blank" ) ;
146
157
147
158
throw new ArgumentException ( message , name ) ;
148
159
}
149
160
}
150
161
151
162
/// <summary>
152
- /// Ensures that <paramref name="value"/> is true.
163
+ /// Ensures that <paramref name="value"/> is <c> true</c> .
153
164
/// </summary>
154
165
/// <param name="value">
155
- /// The value to check, must be true.
166
+ /// The value to check, must be <c> true</c> .
156
167
/// </param>
157
168
/// <param name="name">
158
169
/// The name of the parameter the value is taken from, must not be
159
170
/// blank.
160
171
/// </param>
161
172
/// <param name="message">
162
- /// The message to provide to the exception if <paramref name="collection "/>
163
- /// is false, must not be blank.
173
+ /// The message to provide to the exception if <paramref name="value "/>
174
+ /// is <c> false</c> , must not be blank.
164
175
/// </param>
165
176
/// <exception cref="ArgumentException">
166
- /// Thrown if <paramref name="value"/> is false, or if <paramref name="name"/>
177
+ /// Thrown if <paramref name="value"/> is <c> false</c> , or if <paramref name="name"/>
167
178
/// or <paramref name="message"/> are blank.
168
179
/// </exception>
169
180
public static void CheckTrue ( bool value , string name , string message )
170
181
{
171
182
if ( ! value )
172
183
{
173
- CheckNotBlank ( name , " name" , "name must not be blank" ) ;
174
- CheckNotBlank ( message , " message" , "message must not be blank" ) ;
184
+ CheckNotBlank ( name , nameof ( name ) , "name must not be blank" ) ;
185
+ CheckNotBlank ( message , nameof ( message ) , "message must not be blank" ) ;
175
186
176
187
throw new ArgumentException ( message , name ) ;
177
188
}
178
189
}
179
190
180
191
/// <summary>
181
- /// Ensures that <paramref name="value"/> is false.
192
+ /// Ensures that <paramref name="value"/> is <c> false</c> .
182
193
/// </summary>
183
194
/// <param name="value">
184
- /// The value to check, must be false.
195
+ /// The value to check, must be <c> false</c> .
185
196
/// </param>
186
197
/// <param name="name">
187
198
/// The name of the parameter the value is taken from, must not be
188
199
/// blank.
189
200
/// </param>
190
201
/// <param name="message">
191
- /// The message to provide to the exception if <paramref name="collection "/>
192
- /// is true, must not be blank.
202
+ /// The message to provide to the exception if <paramref name="value "/>
203
+ /// is <c> true</c> , must not be blank.
193
204
/// </param>
194
205
/// <exception cref="ArgumentException">
195
- /// Thrown if <paramref name="value"/> is true, or if <paramref name="name"/>
206
+ /// Thrown if <paramref name="value"/> is <c> true</c> , or if <paramref name="name"/>
196
207
/// or <paramref name="message"/> are blank.
197
208
/// </exception>
198
209
public static void CheckFalse ( bool value , string name , string message )
199
210
{
200
211
if ( value )
201
212
{
202
- CheckNotBlank ( name , " name" , "name must not be blank" ) ;
203
- CheckNotBlank ( message , " message" , "message must not be blank" ) ;
213
+ CheckNotBlank ( name , nameof ( name ) , "name must not be blank" ) ;
214
+ CheckNotBlank ( message , nameof ( message ) , "message must not be blank" ) ;
204
215
205
216
throw new ArgumentException ( message , name ) ;
206
217
}
@@ -220,8 +231,8 @@ public static void CheckTypeMatches(Type expectedType, object value, string name
220
231
bool assignable = expectedType . IsAssignableFrom ( value . GetType ( ) ) ;
221
232
if ( ! assignable )
222
233
{
223
- CheckNotBlank ( name , " name" , "name must not be blank" ) ;
224
- CheckNotBlank ( message , " message" , "message must not be blank" ) ;
234
+ CheckNotBlank ( name , nameof ( name ) , "name must not be blank" ) ;
235
+ CheckNotBlank ( message , nameof ( message ) , "message must not be blank" ) ;
225
236
226
237
throw new ArgumentException ( message , name ) ;
227
238
}
@@ -234,11 +245,11 @@ public static void CheckLess(TimeSpan value, TimeSpan maxValue, string name)
234
245
throw new ArgumentOutOfRangeException ( name , string . Format ( "Arguments {0} must be less than maxValue" , name ) ) ;
235
246
}
236
247
237
- public static void CheckNull < T > ( T value , string name )
248
+ public static void CheckNull < T > ( T value , string name ) where T : class
238
249
{
239
250
if ( value == null )
240
251
return ;
241
252
throw new ArgumentException ( string . Format ( "{0} must not be null" , name ) , name ) ;
242
253
}
243
254
}
244
- }
255
+ }
0 commit comments