Switch to ArgumentNullException.ThrowIfNull extension

このコミットが含まれているのは:
Sergio Pedri committed 2023-02-25 16:20:04 +01:00
1 親 cb1e4c3b96
コミット 6f7bbff6cf
24ファイル変更
+556 -503

検索条件に一致するファイルはありません

+62
ファイルの表示
@@ -0,0 +1,62 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace System;
/// <summary>
/// Throw helper extensions for <see cref="ArgumentNullException"/>.
/// </summary>
internal static class ArgumentNullExceptionExtensions
{
/// <summary>
/// Throws an <see cref="ArgumentNullException"/> for a given parameter name.
/// </summary>
/// <param name="_">Dummy value to invoke the extension upon (always pass <see langword="null"/>.</param>
/// <param name="parameterName">The name of the parameter to report in the exception.</param>
/// <exception cref="ArgumentNullException">Thrown with <paramref name="parameterName"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[DoesNotReturn]
public static void Throw(this ArgumentNullException? _, string? parameterName)
{
Throw(parameterName);
}
/// <summary>
/// Throws an <see cref="ArgumentNullException"/> if <paramref name="argument"/> is <see langword="null"/>.
/// </summary>
/// <param name="_">Dummy value to invoke the extension upon (always pass <see langword="null"/>.</param>
/// <param name="argument">The reference type argument to validate as non-<see langword="null"/>.</param>
/// <param name="parameterName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="argument"/> is <see langword="null"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfNull(this ArgumentNullException? _, [NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? parameterName = null)
{
if (argument is null)
{
Throw(parameterName);
}
}
/// <summary>
/// Throws an <see cref="ArgumentNullException"/> if <paramref name="argument"/> is <see langword="null"/>.
/// </summary>
/// <param name="_">Dummy value to invoke the extension upon (always pass <see langword="null"/>.</param>
/// <param name="argument">The pointer argument to validate as non-<see langword="null"/>.</param>
/// <param name="parameterName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="argument"/> is <see langword="null"/>.</exception>
[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);
}
}
/// <inheritdoc cref="Throw(ArgumentNullException?, string?)"/>
[DoesNotReturn]
public static void Throw(string? parameterName)
{
throw new ArgumentNullException(parameterName);
}
}