Last active
November 27, 2021 13:07
-
-
Save gsscoder/1394c4c845f214d4556b299e3abbe59f to your computer and use it in GitHub Desktop.
C# arguments validation guard class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Runtime.CompilerServices; | |
static class Guard | |
{ | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static void AgainstNull(string argumentName, object value) | |
{ | |
if (value == null) throw new ArgumentNullException(argumentName, $"{argumentName} cannot be null."); | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static void AgainstDefault<T>(string argumentName, T value) | |
where T : struct | |
{ | |
if (value.Equals(default(T))) throw new ArgumentNullException(argumentName, $"{argumentName} cannot be default."); | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static void AgainstWhiteSpace(string argumentName, string value) | |
{ | |
if (value.Any(c => char.IsWhiteSpace(c))) throw new ArgumentException( | |
$"{argumentName} cannot be made of or contains only white spaces.", argumentName); | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static void AgainstEmptyWhiteSpace(string argumentName, string value) | |
{ | |
if (value.Trim() == string.Empty) throw new ArgumentException( | |
$"{argumentName} cannot be empty or contains only white spaces.", argumentName); | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static void AgainstAnyEmptyWhiteSpace(string argumentName, string[] value) | |
{ | |
if (value.Any(s => s.Trim() == string.Empty)) throw new ArgumentException( | |
$"{argumentName} items cannot be empty or contains only white spaces.", argumentName); | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static void AgainstNegative(string argumentName, int value) | |
{ | |
if (value < 0) throw new ArgumentException(argumentName, $"{argumentName} cannot be lesser than zero."); | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static void AgainstNegativeZero(string argumentName, int value) | |
{ | |
if (value < 1) throw new ArgumentException(argumentName, $"{argumentName} cannot be lesser than one."); | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static void AgainstOdd(string argumentName, int value) | |
{ | |
if (value % 2 != 0) throw new ArgumentException(argumentName, $"{argumentName} cannot be odd."); | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static void AgainstArraySize<T>(string argumentName, ushort length, T[] value) | |
{ | |
if (value.Length < length) throw new ArgumentException( | |
$"{argumentName} cannot contain less than {length} elements.", argumentName); | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static void AgainstEmptyArray<T>(string argumentName, T[] value) | |
{ | |
if (value.Length == 0) throw new ArgumentException($"{argumentName} cannot be empty.", argumentName); | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static void AgainstExceptGuid(string argumentName, string value) | |
{ | |
try { Guid.Parse(value); } catch { | |
throw new ArgumentException($"{argumentName} must be a correctly formatted GUID.", argumentName); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment