Skip to content

Instantly share code, notes, and snippets.

@bjorg
Last active February 15, 2022 00:08
Show Gist options
  • Save bjorg/9e2cf008ec1246d638de496d3bc41214 to your computer and use it in GitHub Desktop.
Save bjorg/9e2cf008ec1246d638de496d3bc41214 to your computer and use it in GitHub Desktop.
ArgumentAssertException Utility class to validate arguments
namespace System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
public class ArgumentAssertException : ArgumentException {
//--- Class Methods ---
public static void Assert(
[DoesNotReturnIf(false)] bool expression,
[CallerArgumentExpression("expression")] string? expressionText = default
) {
if(!expression) {
Throw(expressionText);
}
}
[DoesNotReturn]
internal static void Throw(string? message) => throw new ArgumentAssertException(message);
//--- Constructors ---
public ArgumentAssertException() {}
public ArgumentAssertException(string? message) : base(message) {}
}
@bjorg
Copy link
Author

bjorg commented Feb 15, 2022

This is now available as a Nuget package: https://www.nuget.org/packages/ArgumentAssert/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment