using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace System;
///
/// Throw helper extensions for .
///
internal static class ArgumentNullExceptionExtensions
{
///
/// Throws an for a given parameter name.
///
/// Dummy value to invoke the extension upon (always pass .
/// The name of the parameter to report in the exception.
/// Thrown with .
[DoesNotReturn]
public static void Throw(this ArgumentNullException? _, string? parameterName)
{
throw new ArgumentNullException(parameterName);
}
///
/// Throws an if is .
///
/// Dummy value to invoke the extension upon (always pass .
/// The reference type argument to validate as non-.
/// The name of the parameter with which corresponds.
/// Thrown if is .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfNull(this ArgumentNullException? _, [NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? parameterName = null)
{
if (argument is null)
{
Throw(parameterName);
}
}
///
/// Throws an if is .
///
/// Dummy value to invoke the extension upon (always pass .
/// The pointer argument to validate as non-.
/// The name of the parameter with which corresponds.
/// Thrown if is .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void ThrowIfNull(this ArgumentNullException? _, [NotNull] void* argument, [CallerArgumentExpression(nameof(argument))] string? parameterName = null)
{
if (argument is null)
{
Throw(parameterName);
}
}
///
[DoesNotReturn]
private static void Throw(string? parameterName)
{
throw new ArgumentNullException(parameterName);
}
}