-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathArgumentNullThrowHelper.cs
40 lines (35 loc) · 1.38 KB
/
ArgumentNullThrowHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace Microsoft.AspNetCore.Shared;
#nullable enable
internal static partial class ArgumentNullThrowHelper
{
/// <summary>Throws an <see cref="ArgumentNullException"/> if <paramref name="argument"/> is null.</summary>
/// <param name="argument">The reference type argument to validate as non-null.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
public static void ThrowIfNull(
#if INTERNAL_NULLABLE_ATTRIBUTES || NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
[NotNull]
#endif
object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
#if !NET7_0_OR_GREATER || NETSTANDARD || NETFRAMEWORK
if (argument is null)
{
Throw(paramName);
}
#else
ArgumentNullException.ThrowIfNull(argument, paramName);
#endif
}
#if !NET7_0_OR_GREATER || NETSTANDARD || NETFRAMEWORK
#if INTERNAL_NULLABLE_ATTRIBUTES || NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
[DoesNotReturn]
#endif
internal static void Throw(string? paramName) =>
throw new ArgumentNullException(paramName);
#endif
}