Skip to content

Commit 717c7ac

Browse files
Backport ThrowIfNullOrEmpty and ThrowIfNull
1 parent efd4845 commit 717c7ac

File tree

7 files changed

+106
-4
lines changed

7 files changed

+106
-4
lines changed

src/Abstractions/src/Asp.Versioning.Abstractions/Asp.Versioning.Abstractions.csproj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@
3030
<Compile Include="$(BackportDir)Array.cs" Visible="false" />
3131
</ItemGroup>
3232

33-
<ItemGroup Condition=" ('$(TargetFramework)' == 'netstandard1.0') OR ('$(TargetFramework)' == 'netstandard2.0') ">
33+
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETStandard' ">
34+
<Compile Include="$(BackportDir)ArgumentNullException.cs" Visible="false" />
35+
<Compile Include="$(BackportDir)CallerArgumentExpressionAttribute.cs" Visible="false" />
36+
<Compile Include="$(BackportDir)BitOperations.cs" Visible="false" />
37+
<Compile Include="$(BackportDir)HashCode.cs" Visible="false" />
3438
<Compile Include="$(BackportDir)NullableAttributes.cs" Visible="false" />
3539
</ItemGroup>
3640

37-
<ItemGroup Condition=" '$(TargetFramework)' != 'net7.0' ">
38-
<Compile Include="$(BackportDir)BitOperations.cs" Visible="false" />
41+
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETStandard' ">
42+
<Using Include="Backport.ArgumentNullException" Alias="ArgumentNullException" />
3943
<Compile Include="$(BackportDir)HashCode.cs" Visible="false" />
4044
</ItemGroup>
4145

src/AspNet/OData/src/Asp.Versioning.WebApi.OData.ApiExplorer/Asp.Versioning.WebApi.OData.ApiExplorer.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
</ItemGroup>
1616

1717
<ItemGroup>
18+
<Compile Include="$(BackportDir)ArgumentException.cs" Visible="false" />
19+
<Compile Include="$(BackportDir)ArgumentNullException.cs" Visible="false" />
20+
<Compile Include="$(BackportDir)CallerArgumentExpressionAttribute.cs" Visible="false" />
1821
<Compile Include="$(BackportDir)BitOperations.cs" Visible="false" />
1922
<Compile Include="$(BackportDir)HashCode.cs" Visible="false" />
2023
<Compile Include="$(BackportDir)NullableAttributes.cs" Visible="false" />
@@ -28,6 +31,13 @@
2831
<None Include="net45\**\*.cs" />
2932
</ItemGroup>
3033

34+
<ItemGroup>
35+
<Using Include="Backport.ArgumentException" Alias="ArgumentException" />
36+
<Using Include="Backport.ArgumentNullException" Alias="ArgumentNullException" />
37+
<Using Include="Asp.Versioning.SR" Alias="BackportSR" />
38+
<Using Include="Asp.Versioning.ODataExpSR" Alias="Format" />
39+
</ItemGroup>
40+
3141
<ItemGroup>
3242
<Reference Include="System.ComponentModel.DataAnnotations" />
3343
</ItemGroup>

src/AspNet/WebApi/src/Asp.Versioning.WebApi/Asp.Versioning.WebApi.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,22 @@
1515
</ItemGroup>
1616

1717
<ItemGroup>
18+
<Compile Include="$(BackportDir)ArgumentException.cs" Visible="false" />
19+
<Compile Include="$(BackportDir)ArgumentNullException.cs" Visible="false" />
20+
<Compile Include="$(BackportDir)CallerArgumentExpressionAttribute.cs" Visible="false" />
1821
<Compile Include="$(BackportDir)BitOperations.cs" Visible="false" />
1922
<Compile Include="$(BackportDir)HashCode.cs" Visible="false" />
2023
<Compile Include="$(BackportDir)NullableAttributes.cs" Visible="false" />
2124
</ItemGroup>
2225

26+
<ItemGroup>
27+
<Using Include="Backport.ArgumentException" Alias="ArgumentException" />
28+
<Using Include="Backport.ArgumentNullException" Alias="ArgumentNullException" />
29+
<Using Include="Asp.Versioning.SR" Alias="BackportSR" />
30+
<Using Include="Asp.Versioning.CommonSR" Alias="Format" />
31+
<Using Include="Asp.Versioning.MvcSR" Alias="MvcFormat" />
32+
</ItemGroup>
33+
2334
<ItemGroup>
2435
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="[5.2.7,6.0.0)" />
2536
</ItemGroup>

src/Client/src/Asp.Versioning.Http.Client/Asp.Versioning.Http.Client.csproj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
<Compile Include="$(BackportDir)Array.cs" Visible="false" />
2020
</ItemGroup>
2121

22-
<ItemGroup Condition=" ('$(TargetFramework)' == 'netstandard1.1') OR ('$(TargetFramework)' == 'netstandard2.0') ">
22+
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETStandard' ">
23+
<Compile Include="$(BackportDir)ArgumentException.cs" Visible="false" />
24+
<Compile Include="$(BackportDir)ArgumentNullException.cs" Visible="false" />
25+
<Compile Include="$(BackportDir)CallerArgumentExpressionAttribute.cs" Visible="false" />
26+
<Compile Include="$(BackportDir)NullableAttributes.cs" Visible="false" />
2327
<Compile Include="$(BackportDir)StringExtensions.cs" Visible="false" />
2428
</ItemGroup>
2529

@@ -30,6 +34,11 @@
3034
<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard1.1' ">
3135
<PackageReference Include="Microsoft.Extensions.Http" Version="$(DotNetReleasePackageVersion)" />
3236
</ItemGroup>
37+
38+
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETStandard' ">
39+
<Using Include="Backport.ArgumentException" Alias="ArgumentException" />
40+
<Using Include="Backport.ArgumentNullException" Alias="ArgumentNullException" />
41+
<Using Include="Asp.Versioning.Http.SR" Alias="BackportSR" />
3342
</ItemGroup>
3443

3544
<ItemGroup>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
3+
// REF: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/ArgumentException.cs
4+
namespace Backport;
5+
6+
using System.Runtime.CompilerServices;
7+
8+
internal static class ArgumentException
9+
{
10+
/// <summary>Throws an exception if <paramref name="argument"/> is null or empty.</summary>
11+
/// <param name="argument">The string argument to validate as non-null and non-empty.</param>
12+
/// <param name="paramName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
13+
/// <exception cref="ArgumentNullException"><paramref name="argument"/> is null.</exception>
14+
/// <exception cref="ArgumentException"><paramref name="argument"/> is empty.</exception>
15+
[MethodImpl( MethodImplOptions.AggressiveInlining )]
16+
public static void ThrowIfNullOrEmpty( string? argument, [CallerArgumentExpression( nameof( argument ) )] string? paramName = null )
17+
{
18+
if ( string.IsNullOrEmpty( argument ) )
19+
{
20+
ThrowNullOrEmptyException( argument, paramName );
21+
}
22+
}
23+
24+
[DoesNotReturn]
25+
[MethodImpl( MethodImplOptions.AggressiveInlining )]
26+
private static void ThrowNullOrEmptyException( string? argument, string? paramName )
27+
{
28+
ArgumentNullException.ThrowIfNull( argument, paramName );
29+
throw new System.ArgumentException( BackportSR.Argument_EmptyString, paramName );
30+
}
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
3+
// REF: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/ArgumentNullException.cs
4+
namespace Backport;
5+
6+
using System.Runtime.CompilerServices;
7+
8+
internal static class ArgumentNullException
9+
{
10+
/// <summary>Throws an <see cref="System.ArgumentNullException"/> if <paramref name="argument"/> is null.</summary>
11+
/// <param name="argument">The reference type argument to validate as non-null.</param>
12+
/// <param name="paramName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
13+
[MethodImpl( MethodImplOptions.AggressiveInlining )]
14+
public static void ThrowIfNull( [NotNull] object? argument, [CallerArgumentExpression( nameof( argument ) )] string? paramName = null )
15+
{
16+
if ( argument is null )
17+
{
18+
throw new System.ArgumentNullException( paramName );
19+
}
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
3+
// REF: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/CallerArgumentExpressionAttribute.cs
4+
namespace System.Runtime.CompilerServices
5+
{
6+
[AttributeUsage( AttributeTargets.Parameter, AllowMultiple = false, Inherited = false )]
7+
internal sealed class CallerArgumentExpressionAttribute : Attribute
8+
{
9+
public CallerArgumentExpressionAttribute( string parameterName )
10+
{
11+
ParameterName = parameterName;
12+
}
13+
14+
public string ParameterName { get; }
15+
}
16+
}

0 commit comments

Comments
 (0)