Skip to content

Commit

Permalink
Add ArgumentNullException.ThrowIfNull (#55594)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Jul 14, 2021
1 parent 56d9474 commit 4e9d89a
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 110 deletions.
70 changes: 31 additions & 39 deletions src/libraries/System.Net.WebClient/src/System/Net/WebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public Encoding Encoding
get { return _encoding; }
set
{
ThrowIfNull(value, nameof(value));
ArgumentNullException.ThrowIfNull(value, nameof(value));
_encoding = value;
}
}
Expand Down Expand Up @@ -280,7 +280,7 @@ public byte[] DownloadData(string address) =>

public byte[] DownloadData(Uri address)
{
ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(address, nameof(address));

StartOperation();
try
Expand Down Expand Up @@ -320,8 +320,8 @@ public void DownloadFile(string address, string fileName) =>

public void DownloadFile(Uri address, string fileName)
{
ThrowIfNull(address, nameof(address));
ThrowIfNull(fileName, nameof(fileName));
ArgumentNullException.ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(fileName, nameof(fileName));

WebRequest? request = null;
FileStream? fs = null;
Expand Down Expand Up @@ -359,7 +359,7 @@ public Stream OpenRead(string address) =>

public Stream OpenRead(Uri address)
{
ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(address, nameof(address));

WebRequest? request = null;
StartOperation();
Expand Down Expand Up @@ -392,7 +392,7 @@ public Stream OpenWrite(string address, string? method) =>

public Stream OpenWrite(Uri address, string? method)
{
ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(address, nameof(address));
if (method == null)
{
method = MapToDefaultMethod(address);
Expand Down Expand Up @@ -432,8 +432,8 @@ public byte[] UploadData(string address, string? method, byte[] data) =>

public byte[] UploadData(Uri address, string? method, byte[] data)
{
ThrowIfNull(address, nameof(address));
ThrowIfNull(data, nameof(data));
ArgumentNullException.ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(data, nameof(data));
if (method == null)
{
method = MapToDefaultMethod(address);
Expand Down Expand Up @@ -553,8 +553,8 @@ public byte[] UploadFile(string address, string? method, string fileName) =>

public byte[] UploadFile(Uri address, string? method, string fileName)
{
ThrowIfNull(address, nameof(address));
ThrowIfNull(fileName, nameof(fileName));
ArgumentNullException.ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(fileName, nameof(fileName));
if (method == null)
{
method = MapToDefaultMethod(address);
Expand Down Expand Up @@ -626,8 +626,8 @@ public byte[] UploadValues(string address, string? method, NameValueCollection d

public byte[] UploadValues(Uri address, string? method, NameValueCollection data)
{
ThrowIfNull(address, nameof(address));
ThrowIfNull(data, nameof(data));
ArgumentNullException.ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(data, nameof(data));
if (method == null)
{
method = MapToDefaultMethod(address);
Expand Down Expand Up @@ -665,8 +665,8 @@ public string UploadString(string address, string? method, string data) =>

public string UploadString(Uri address, string? method, string data)
{
ThrowIfNull(address, nameof(address));
ThrowIfNull(data, nameof(data));
ArgumentNullException.ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(data, nameof(data));
if (method == null)
{
method = MapToDefaultMethod(address);
Expand All @@ -691,7 +691,7 @@ public string DownloadString(string address) =>

public string DownloadString(Uri address)
{
ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(address, nameof(address));

StartOperation();
try
Expand Down Expand Up @@ -781,7 +781,7 @@ private void CopyHeadersTo(WebRequest request)

private Uri GetUri(string address)
{
ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(address, nameof(address));

Uri? uri;
if (_baseAddress != null)
Expand All @@ -801,7 +801,7 @@ private Uri GetUri(string address)

private Uri GetUri(Uri address)
{
ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(address, nameof(address));

Uri? uri = address;

Expand Down Expand Up @@ -1297,7 +1297,7 @@ public void OpenReadAsync(Uri address) =>

public void OpenReadAsync(Uri address, object? userToken)
{
ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(address, nameof(address));

AsyncOperation asyncOp = StartAsyncOperation(userToken);
try
Expand Down Expand Up @@ -1335,7 +1335,7 @@ public void OpenWriteAsync(Uri address, string? method) =>

public void OpenWriteAsync(Uri address, string? method, object? userToken)
{
ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(address, nameof(address));
if (method == null)
{
method = MapToDefaultMethod(address);
Expand Down Expand Up @@ -1396,7 +1396,7 @@ public void DownloadStringAsync(Uri address) =>

public void DownloadStringAsync(Uri address, object? userToken)
{
ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(address, nameof(address));

AsyncOperation asyncOp = StartAsyncOperation(userToken);
try
Expand All @@ -1422,7 +1422,7 @@ public void DownloadDataAsync(Uri address) =>

public void DownloadDataAsync(Uri address, object? userToken)
{
ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(address, nameof(address));

AsyncOperation asyncOp = StartAsyncOperation(userToken);
try
Expand All @@ -1448,8 +1448,8 @@ public void DownloadFileAsync(Uri address, string fileName) =>

public void DownloadFileAsync(Uri address, string fileName, object? userToken)
{
ThrowIfNull(address, nameof(address));
ThrowIfNull(fileName, nameof(fileName));
ArgumentNullException.ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(fileName, nameof(fileName));

FileStream? fs = null;
AsyncOperation asyncOp = StartAsyncOperation(userToken);
Expand All @@ -1474,8 +1474,8 @@ public void UploadStringAsync(Uri address, string? method, string data) =>

public void UploadStringAsync(Uri address, string? method, string data, object? userToken)
{
ThrowIfNull(address, nameof(address));
ThrowIfNull(data, nameof(data));
ArgumentNullException.ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(data, nameof(data));
if (method == null)
{
method = MapToDefaultMethod(address);
Expand Down Expand Up @@ -1525,8 +1525,8 @@ public void UploadDataAsync(Uri address, string? method, byte[] data) =>

public void UploadDataAsync(Uri address, string? method, byte[] data, object? userToken)
{
ThrowIfNull(address, nameof(address));
ThrowIfNull(data, nameof(data));
ArgumentNullException.ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(data, nameof(data));
if (method == null)
{
method = MapToDefaultMethod(address);
Expand Down Expand Up @@ -1566,8 +1566,8 @@ public void UploadFileAsync(Uri address, string? method, string fileName) =>

public void UploadFileAsync(Uri address, string? method, string fileName, object? userToken)
{
ThrowIfNull(address, nameof(address));
ThrowIfNull(fileName, nameof(fileName));
ArgumentNullException.ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(fileName, nameof(fileName));
if (method == null)
{
method = MapToDefaultMethod(address);
Expand Down Expand Up @@ -1605,8 +1605,8 @@ public void UploadValuesAsync(Uri address, string? method, NameValueCollection d

public void UploadValuesAsync(Uri address, string? method, NameValueCollection data, object? userToken)
{
ThrowIfNull(address, nameof(address));
ThrowIfNull(data, nameof(data));
ArgumentNullException.ThrowIfNull(address, nameof(address));
ArgumentNullException.ThrowIfNull(data, nameof(data));
if (method == null)
{
method = MapToDefaultMethod(address);
Expand Down Expand Up @@ -1940,14 +1940,6 @@ private void PostProgressChanged(AsyncOperation asyncOp, ProgressData progress)
}
}

private static void ThrowIfNull(object argument, string parameterName)
{
if (argument == null)
{
throw new ArgumentNullException(parameterName);
}
}

#region Supporting Types
private sealed class ProgressData
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
**
=============================================================================*/

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;

namespace System
Expand Down Expand Up @@ -50,5 +52,20 @@ public ArgumentNullException(string? paramName, string? message)
protected ArgumentNullException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}

/// <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([NotNull] object? argument, [CallerArgumentExpression("argument")] string? paramName = null)
{
if (argument is null)
{
Throw(paramName);
}
}

[DoesNotReturn]
private static void Throw(string? paramName) =>
throw new ArgumentNullException(paramName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Compile Include="System\Reflection\Requires.cs" />
<Compile Include="System\Reflection\TypeExtensions.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(CoreLibProject)" />
</ItemGroup>
</Project>
</Project>

This file was deleted.

Loading

0 comments on commit 4e9d89a

Please sign in to comment.