-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCheck.cs
More file actions
250 lines (247 loc) · 9.25 KB
/
Copy pathCheck.cs
File metadata and controls
250 lines (247 loc) · 9.25 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System.Diagnostics;
using System.Globalization;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace BytecodeApi;
internal static class Check
{
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Argument(bool condition, string? parameterName, string message)
{
if (!condition)
{
throw new ArgumentException(message, parameterName);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ArgumentNull<T>([NotNull] T? parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null)
{
if (parameter == null)
{
throw new ArgumentNullException(parameterName);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InvalidOperation(bool condition, string message)
{
if (!condition)
{
throw new InvalidOperationException(message);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ArgumentOutOfRange(bool condition, string? parameterName, string message)
{
if (!condition)
{
throw new ArgumentOutOfRangeException(parameterName, message);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IndexOutOfRange(int index, int count)
{
if (index < 0 || index >= count)
{
throw new IndexOutOfRangeException();
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Format(bool condition, string message)
{
if (!condition)
{
throw new FormatException(message);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InvalidCast(bool condition, string? parameterName)
{
if (!condition)
{
throw new InvalidCastException(string.Format(CultureInfo.InvariantCulture, ExceptionMessages.InvalidCast, parameterName));
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Overflow(bool condition)
{
if (!condition)
{
throw new OverflowException();
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void DivideByZero<T>(T parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null) where T : INumber<T>
{
if (T.IsZero(parameter))
{
throw new DivideByZeroException(string.Format(CultureInfo.InvariantCulture, ExceptionMessages.DivideByZero, parameterName));
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void DivideByZero(long parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null)
{
if (parameter == 0)
{
throw new DivideByZeroException(string.Format(CultureInfo.InvariantCulture, ExceptionMessages.DivideByZero, parameterName));
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void FileNotFound(string? path)
{
if (!File.Exists(path))
{
throw new FileNotFoundException(null, path);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void DirectoryNotFound(string? path)
{
if (!Directory.Exists(path))
{
throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, ExceptionMessages.DirectoryNotFound, path));
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void FileOrDirectoryNotFound(string? path)
{
if (!File.Exists(path) && !Directory.Exists(path))
{
throw new FileNotFoundException(null, path);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void TargetParameterCount(int count1, int count2)
{
if (count1 != count2)
{
throw new TargetParameterCountException();
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ObjectDisposed<T>(bool disposed)
{
if (disposed)
{
throw new ObjectDisposedException(typeof(T).FullName);
}
}
public static class ArgumentEx
{
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void StringNotEmpty(string? parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null)
{
if (parameter == "")
{
throw new ArgumentException(ExceptionMessages.Argument.StringNotEmpty, parameterName);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void StringNotEmptyOrWhiteSpace(string parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null)
{
if (parameter == "" || parameter.Any(char.IsWhiteSpace))
{
throw new ArgumentException(ExceptionMessages.Argument.StringNotEmptyOrWhiteSpace, parameterName);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void OffsetAndLengthOutOfBounds(int offset, int count, int length)
{
if (offset + count > length)
{
throw new ArgumentException(ExceptionMessages.Argument.OffsetAndLengthOutOfBounds);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ArrayElementsRequired<T>(IEnumerable<T?> parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null)
{
if (!parameter.Any())
{
throw new ArgumentException(ExceptionMessages.Argument.ArrayElementsRequired, parameterName);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void EnumerableElementsRequired(bool any, string? parameterName)
{
if (!any)
{
throw new ArgumentException(ExceptionMessages.Argument.EnumerableElementsRequired, parameterName);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ArrayValuesNotNull<T>(IEnumerable<T?> parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null)
{
if (parameter.Any(itm => itm == null))
{
throw new ArgumentException(ExceptionMessages.Argument.ArrayValuesNotNull, parameterName);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ArrayValuesNotStringEmpty(IEnumerable<string> parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null)
{
if (parameter.Any(itm => itm == ""))
{
throw new ArgumentException(ExceptionMessages.Argument.ArrayValuesNotStringEmpty, parameterName);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Handle(nint handle, [CallerArgumentExpression(nameof(handle))] string? parameterName = null)
{
if (handle is 0 or -1)
{
throw new ArgumentException(ExceptionMessages.Argument.InvalidHandle, parameterName);
}
}
}
public static class ArgumentOutOfRangeEx
{
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void GreaterEqual0<T>(T parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null) where T : INumber<T>
{
if (parameter < T.Zero)
{
throw new ArgumentOutOfRangeException(parameterName, ExceptionMessages.ArgumentOutOfRange.GreaterEqual0);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void GreaterEqual0(TimeSpan parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null)
{
if (parameter < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(parameterName, ExceptionMessages.ArgumentOutOfRange.GreaterEqual0);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Greater0<T>(T parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null) where T : INumber<T>
{
if (parameter <= T.Zero)
{
throw new ArgumentOutOfRangeException(parameterName, ExceptionMessages.ArgumentOutOfRange.Greater0);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Greater0(TimeSpan parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null)
{
if (parameter <= TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(parameterName, ExceptionMessages.ArgumentOutOfRange.Greater0);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Between0And1<T>(T parameter, [CallerArgumentExpression(nameof(parameter))] string? parameterName = null) where T : INumber<T>
{
if (parameter < T.Zero || parameter > T.One)
{
throw new ArgumentOutOfRangeException(parameterName, ExceptionMessages.ArgumentOutOfRange.Between0And1);
}
}
[DebuggerHidden, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void GreaterEqualValue<T>(T parameter1, T parameter2, [CallerArgumentExpression(nameof(parameter1))] string parameter1Name = null!, [CallerArgumentExpression(nameof(parameter2))] string parameter2Name = null!) where T : struct, IComparable
{
if (parameter1.CompareTo(parameter2) < 0)
{
throw new ArgumentOutOfRangeException(parameter1Name, string.Format(CultureInfo.InvariantCulture, ExceptionMessages.ArgumentOutOfRange.GreaterEqualValue, parameter2Name));
}
}
}
}