Last active
February 15, 2022 00:08
-
-
Save bjorg/9e2cf008ec1246d638de496d3bc41214 to your computer and use it in GitHub Desktop.
ArgumentAssertException Utility class to validate arguments
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
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) {} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is now available as a Nuget package: https://www.nuget.org/packages/ArgumentAssert/