Stashbox by Peter Csajtai

<PackageReference Include="Stashbox" Version="3.2.9-preview-600" />

 Shield

public static class Shield
Represents a utility class for input validation.
using System; using System.Linq.Expressions; namespace Stashbox.Utils { public static class Shield { private static string GetMemberName<T>(Expression<Func<T>> memberExpression) { MemberExpression obj = memberExpression.Body as MemberExpression; if (obj == null) throw new ArgumentException("The given expression is not a MemberExpression."); return obj.Member.Name; } public static void EnsureNotNull<T>(Expression<Func<T>> memberExpression) where T : class { if (memberExpression == null) throw new ArgumentNullException("memberExpression"); if (memberExpression.Compile()() == null) throw new ArgumentNullException(GetMemberName(memberExpression)); } public static void EnsureNotNull<T>(T obj, string parameterName) where T : class { if (obj == null) throw new ArgumentNullException(parameterName); } public static void EnsureNotNull<T>(Expression<Func<T>> memberExpression, string message) where T : class { if (memberExpression == null) throw new ArgumentNullException("memberExpression"); if (memberExpression.Compile()() == null) throw new ArgumentNullException(GetMemberName(memberExpression), message); } public static void EnsureNotNull<T>(T obj, string parameterName, string message) where T : class { if (obj == null) throw new ArgumentNullException(parameterName, message); } public static void EnsureNotNullOrEmpty(Expression<Func<string>> memberExpression) { if (memberExpression == null) throw new ArgumentNullException("memberExpression"); if (string.IsNullOrWhiteSpace(memberExpression.Compile()())) throw new ArgumentException(string.Empty, GetMemberName(memberExpression)); } public static void EnsureNotNullOrEmpty(string obj, string parameterName) { if (string.IsNullOrWhiteSpace(obj)) throw new ArgumentException(string.Empty, parameterName); } public static void EnsureGreaterThan(int actual, int reference) { if (reference >= actual) throw new ArgumentException($"""{new object[1] { reference }}"); } public static void EnsureTrue(bool condition, string message) { if (!condition) throw new ArgumentException(message); } public static void EnsureTypeOf<TType>(object obj) { if (!(obj is TType)) throw new ArgumentException("obj"); } } }