diff --git a/src/libraries/System.Net.WebClient/src/System/Net/WebClient.cs b/src/libraries/System.Net.WebClient/src/System/Net/WebClient.cs
index 27289121acd006..553bf48943d5a7 100644
--- a/src/libraries/System.Net.WebClient/src/System/Net/WebClient.cs
+++ b/src/libraries/System.Net.WebClient/src/System/Net/WebClient.cs
@@ -143,7 +143,7 @@ public Encoding Encoding
get { return _encoding; }
set
{
- ThrowIfNull(value, nameof(value));
+ ArgumentNullException.ThrowIfNull(value, nameof(value));
_encoding = value;
}
}
@@ -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
@@ -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;
@@ -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();
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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
@@ -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)
@@ -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;
@@ -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
@@ -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);
@@ -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
@@ -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
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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
{
diff --git a/src/libraries/System.Private.CoreLib/src/System/ArgumentNullException.cs b/src/libraries/System.Private.CoreLib/src/System/ArgumentNullException.cs
index 0f8a1923ad5033..5f8894e291473e 100644
--- a/src/libraries/System.Private.CoreLib/src/System/ArgumentNullException.cs
+++ b/src/libraries/System.Private.CoreLib/src/System/ArgumentNullException.cs
@@ -10,6 +10,8 @@
**
=============================================================================*/
+using System.Diagnostics.CodeAnalysis;
+using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
namespace System
@@ -50,5 +52,20 @@ public ArgumentNullException(string? paramName, string? message)
protected ArgumentNullException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
+
+ /// Throws an if is null.
+ /// The reference type argument to validate as non-null.
+ /// The name of the parameter with which corresponds.
+ 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);
}
}
diff --git a/src/libraries/System.Reflection.TypeExtensions/src/System.Reflection.TypeExtensions.csproj b/src/libraries/System.Reflection.TypeExtensions/src/System.Reflection.TypeExtensions.csproj
index 3e598d9015c00a..e078dd282ebe7b 100644
--- a/src/libraries/System.Reflection.TypeExtensions/src/System.Reflection.TypeExtensions.csproj
+++ b/src/libraries/System.Reflection.TypeExtensions/src/System.Reflection.TypeExtensions.csproj
@@ -5,10 +5,9 @@
$(NetCoreAppCurrent)
-
-
\ No newline at end of file
+
diff --git a/src/libraries/System.Reflection.TypeExtensions/src/System/Reflection/Requires.cs b/src/libraries/System.Reflection.TypeExtensions/src/System/Reflection/Requires.cs
deleted file mode 100644
index 8fa51255d4fe0f..00000000000000
--- a/src/libraries/System.Reflection.TypeExtensions/src/System/Reflection/Requires.cs
+++ /dev/null
@@ -1,16 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-namespace System.Reflection
-{
- internal static class Requires
- {
- internal static void NotNull(object obj, string name)
- {
- if (obj == null)
- {
- throw new ArgumentNullException(name);
- }
- }
- }
-}
diff --git a/src/libraries/System.Reflection.TypeExtensions/src/System/Reflection/TypeExtensions.cs b/src/libraries/System.Reflection.TypeExtensions/src/System/Reflection/TypeExtensions.cs
index 3e3b6ca2961c39..6c020bbfffd4ba 100644
--- a/src/libraries/System.Reflection.TypeExtensions/src/System/Reflection/TypeExtensions.cs
+++ b/src/libraries/System.Reflection.TypeExtensions/src/System/Reflection/TypeExtensions.cs
@@ -11,14 +11,14 @@ public static class TypeExtensions
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] this Type type,
Type[] types)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetConstructor(types);
}
public static ConstructorInfo[] GetConstructors(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] this Type type)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetConstructors();
}
@@ -26,7 +26,7 @@ public static ConstructorInfo[] GetConstructors(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] this Type type,
BindingFlags bindingAttr)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetConstructors(bindingAttr);
}
@@ -39,7 +39,7 @@ public static MemberInfo[] GetDefaultMembers(
| DynamicallyAccessedMemberTypes.PublicConstructors
| DynamicallyAccessedMemberTypes.PublicNestedTypes)] this Type type)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetDefaultMembers();
}
@@ -47,7 +47,7 @@ public static MemberInfo[] GetDefaultMembers(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)] this Type type,
string name)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetEvent(name);
}
@@ -56,14 +56,14 @@ public static MemberInfo[] GetDefaultMembers(
string name,
BindingFlags bindingAttr)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetEvent(name, bindingAttr);
}
public static EventInfo[] GetEvents(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)] this Type type)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetEvents();
}
@@ -71,7 +71,7 @@ public static EventInfo[] GetEvents(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] this Type type,
BindingFlags bindingAttr)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetEvents(bindingAttr);
}
@@ -79,7 +79,7 @@ public static EventInfo[] GetEvents(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] this Type type,
string name)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetField(name);
}
@@ -88,14 +88,14 @@ public static EventInfo[] GetEvents(
string name,
BindingFlags bindingAttr)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetField(name, bindingAttr);
}
public static FieldInfo[] GetFields(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] this Type type)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetFields();
}
@@ -103,20 +103,20 @@ public static FieldInfo[] GetFields(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] this Type type,
BindingFlags bindingAttr)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetFields(bindingAttr);
}
public static Type[] GetGenericArguments(this Type type)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetGenericArguments();
}
public static Type[] GetInterfaces(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] this Type type)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetInterfaces();
}
@@ -130,7 +130,7 @@ public static MemberInfo[] GetMember(
| DynamicallyAccessedMemberTypes.PublicNestedTypes)] this Type type,
string name)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetMember(name);
}
@@ -139,7 +139,7 @@ public static MemberInfo[] GetMember(
string name,
BindingFlags bindingAttr)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetMember(name, bindingAttr);
}
@@ -152,7 +152,7 @@ public static MemberInfo[] GetMembers(
| DynamicallyAccessedMemberTypes.PublicConstructors
| DynamicallyAccessedMemberTypes.PublicNestedTypes)] this Type type)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetMembers();
}
@@ -160,7 +160,7 @@ public static MemberInfo[] GetMembers(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] this Type type,
BindingFlags bindingAttr)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetMembers(bindingAttr);
}
@@ -168,7 +168,7 @@ public static MemberInfo[] GetMembers(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] this Type type,
string name)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetMethod(name);
}
@@ -177,7 +177,7 @@ public static MemberInfo[] GetMembers(
string name,
BindingFlags bindingAttr)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetMethod(name, bindingAttr);
}
@@ -186,14 +186,14 @@ public static MemberInfo[] GetMembers(
string name,
Type[] types)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetMethod(name, types);
}
public static MethodInfo[] GetMethods(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] this Type type)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetMethods();
}
@@ -201,7 +201,7 @@ public static MethodInfo[] GetMethods(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] this Type type,
BindingFlags bindingAttr)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetMethods(bindingAttr);
}
@@ -210,7 +210,7 @@ public static MethodInfo[] GetMethods(
string name,
BindingFlags bindingAttr)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetNestedType(name, bindingAttr);
}
@@ -218,14 +218,14 @@ public static Type[] GetNestedTypes(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] this Type type,
BindingFlags bindingAttr)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetNestedTypes(bindingAttr);
}
public static PropertyInfo[] GetProperties(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] this Type type)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetProperties();
}
@@ -233,7 +233,7 @@ public static PropertyInfo[] GetProperties(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] this Type type,
BindingFlags bindingAttr)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetProperties(bindingAttr);
}
@@ -241,7 +241,7 @@ public static PropertyInfo[] GetProperties(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] this Type type,
string name)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetProperty(name);
}
@@ -250,7 +250,7 @@ public static PropertyInfo[] GetProperties(
string name,
BindingFlags bindingAttr)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetProperty(name, bindingAttr);
}
@@ -259,7 +259,7 @@ public static PropertyInfo[] GetProperties(
string name,
Type? returnType)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetProperty(name, returnType);
}
@@ -269,19 +269,19 @@ public static PropertyInfo[] GetProperties(
Type? returnType,
Type[] types)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.GetProperty(name, returnType, types);
}
public static bool IsAssignableFrom(this Type type, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] Type? c)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.IsAssignableFrom(c);
}
public static bool IsInstanceOfType(this Type type, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] object? o)
{
- Requires.NotNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type, nameof(type));
return type.IsInstanceOfType(o);
}
}
@@ -291,20 +291,20 @@ public static class AssemblyExtensions
[RequiresUnreferencedCode("Types might be removed")]
public static Type[] GetExportedTypes(this Assembly assembly)
{
- Requires.NotNull(assembly, nameof(assembly));
+ ArgumentNullException.ThrowIfNull(assembly, nameof(assembly));
return assembly.GetExportedTypes();
}
public static Module[] GetModules(this Assembly assembly)
{
- Requires.NotNull(assembly, nameof(assembly));
+ ArgumentNullException.ThrowIfNull(assembly, nameof(assembly));
return assembly.GetModules();
}
[RequiresUnreferencedCode("Types might be removed")]
public static Type[] GetTypes(this Assembly assembly)
{
- Requires.NotNull(assembly, nameof(assembly));
+ ArgumentNullException.ThrowIfNull(assembly, nameof(assembly));
return assembly.GetTypes();
}
}
@@ -313,37 +313,37 @@ public static class EventInfoExtensions
{
public static MethodInfo? GetAddMethod(this EventInfo eventInfo)
{
- Requires.NotNull(eventInfo, nameof(eventInfo));
+ ArgumentNullException.ThrowIfNull(eventInfo, nameof(eventInfo));
return eventInfo.GetAddMethod();
}
public static MethodInfo? GetAddMethod(this EventInfo eventInfo, bool nonPublic)
{
- Requires.NotNull(eventInfo, nameof(eventInfo));
+ ArgumentNullException.ThrowIfNull(eventInfo, nameof(eventInfo));
return eventInfo.GetAddMethod(nonPublic);
}
public static MethodInfo? GetRaiseMethod(this EventInfo eventInfo)
{
- Requires.NotNull(eventInfo, nameof(eventInfo));
+ ArgumentNullException.ThrowIfNull(eventInfo, nameof(eventInfo));
return eventInfo.GetRaiseMethod();
}
public static MethodInfo? GetRaiseMethod(this EventInfo eventInfo, bool nonPublic)
{
- Requires.NotNull(eventInfo, nameof(eventInfo));
+ ArgumentNullException.ThrowIfNull(eventInfo, nameof(eventInfo));
return eventInfo.GetRaiseMethod(nonPublic);
}
public static MethodInfo? GetRemoveMethod(this EventInfo eventInfo)
{
- Requires.NotNull(eventInfo, nameof(eventInfo));
+ ArgumentNullException.ThrowIfNull(eventInfo, nameof(eventInfo));
return eventInfo.GetRemoveMethod();
}
public static MethodInfo? GetRemoveMethod(this EventInfo eventInfo, bool nonPublic)
{
- Requires.NotNull(eventInfo, nameof(eventInfo));
+ ArgumentNullException.ThrowIfNull(eventInfo, nameof(eventInfo));
return eventInfo.GetRemoveMethod(nonPublic);
}
}
@@ -358,7 +358,7 @@ public static class MemberInfoExtensions
/// This maybe
public static bool HasMetadataToken(this MemberInfo member)
{
- Requires.NotNull(member, nameof(member));
+ ArgumentNullException.ThrowIfNull(member, nameof(member));
try
{
@@ -380,7 +380,7 @@ public static bool HasMetadataToken(this MemberInfo member)
///
public static int GetMetadataToken(this MemberInfo member)
{
- Requires.NotNull(member, nameof(member));
+ ArgumentNullException.ThrowIfNull(member, nameof(member));
int token = GetMetadataTokenOrZeroOrThrow(member);
@@ -413,7 +413,7 @@ public static class MethodInfoExtensions
{
public static MethodInfo GetBaseDefinition(this MethodInfo method)
{
- Requires.NotNull(method, nameof(method));
+ ArgumentNullException.ThrowIfNull(method, nameof(method));
return method.GetBaseDefinition();
}
}
@@ -422,13 +422,13 @@ public static class ModuleExtensions
{
public static bool HasModuleVersionId(this Module module)
{
- Requires.NotNull(module, nameof(module));
+ ArgumentNullException.ThrowIfNull(module, nameof(module));
return true; // not expected to fail on platforms with Module.ModuleVersionId built-in.
}
public static Guid GetModuleVersionId(this Module module)
{
- Requires.NotNull(module, nameof(module));
+ ArgumentNullException.ThrowIfNull(module, nameof(module));
return module.ModuleVersionId;
}
}
@@ -437,37 +437,37 @@ public static class PropertyInfoExtensions
{
public static MethodInfo[] GetAccessors(this PropertyInfo property)
{
- Requires.NotNull(property, nameof(property));
+ ArgumentNullException.ThrowIfNull(property, nameof(property));
return property.GetAccessors();
}
public static MethodInfo[] GetAccessors(this PropertyInfo property, bool nonPublic)
{
- Requires.NotNull(property, nameof(property));
+ ArgumentNullException.ThrowIfNull(property, nameof(property));
return property.GetAccessors(nonPublic);
}
public static MethodInfo? GetGetMethod(this PropertyInfo property)
{
- Requires.NotNull(property, nameof(property));
+ ArgumentNullException.ThrowIfNull(property, nameof(property));
return property.GetGetMethod();
}
public static MethodInfo? GetGetMethod(this PropertyInfo property, bool nonPublic)
{
- Requires.NotNull(property, nameof(property));
+ ArgumentNullException.ThrowIfNull(property, nameof(property));
return property.GetGetMethod(nonPublic);
}
public static MethodInfo? GetSetMethod(this PropertyInfo property)
{
- Requires.NotNull(property, nameof(property));
+ ArgumentNullException.ThrowIfNull(property, nameof(property));
return property.GetSetMethod();
}
public static MethodInfo? GetSetMethod(this PropertyInfo property, bool nonPublic)
{
- Requires.NotNull(property, nameof(property));
+ ArgumentNullException.ThrowIfNull(property, nameof(property));
return property.GetSetMethod(nonPublic);
}
}
diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs
index 35fe09cc662952..76bbc5dd649585 100644
--- a/src/libraries/System.Runtime/ref/System.Runtime.cs
+++ b/src/libraries/System.Runtime/ref/System.Runtime.cs
@@ -284,6 +284,7 @@ protected ArgumentNullException(System.Runtime.Serialization.SerializationInfo i
public ArgumentNullException(string? paramName) { }
public ArgumentNullException(string? message, System.Exception? innerException) { }
public ArgumentNullException(string? paramName, string? message) { }
+ public static void ThrowIfNull([System.Diagnostics.CodeAnalysis.NotNullAttribute] object? argument, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute("argument")] string? paramName = null) { throw null; }
}
public partial class ArgumentOutOfRangeException : System.ArgumentException
{
diff --git a/src/libraries/System.Runtime/tests/System/ArgumentNullExceptionTests.cs b/src/libraries/System.Runtime/tests/System/ArgumentNullExceptionTests.cs
index 577e7c4ba26edf..78384466f7fb10 100644
--- a/src/libraries/System.Runtime/tests/System/ArgumentNullExceptionTests.cs
+++ b/src/libraries/System.Runtime/tests/System/ArgumentNullExceptionTests.cs
@@ -47,5 +47,32 @@ public static void Ctor_String_String()
Assert.Contains(message, exception.Message);
Assert.Contains(argumentName, exception.Message);
}
+
+ [Fact]
+ public static void ThrowIfNull_NonNull_DoesntThrow()
+ {
+ foreach (object o in new[] { new object(), "", "argument" })
+ {
+ ArgumentNullException.ThrowIfNull(o);
+ ArgumentNullException.ThrowIfNull(o, "paramName");
+ }
+ }
+
+ [Theory]
+ [InlineData(null)]
+ [InlineData("")]
+ [InlineData("name")]
+ public static void ThrowIfNull_Null_ThrowsArgumentNullException(string paramName)
+ {
+ AssertExtensions.Throws(paramName, () => ArgumentNullException.ThrowIfNull(null, paramName));
+ }
+
+ [Fact]
+ [ActiveIssue("https://github.com/dotnet/csharplang/issues/287")]
+ public static void ThrowIfNull_UsesArgumentExpression()
+ {
+ object something = null;
+ AssertExtensions.Throws(nameof(something), () => ArgumentNullException.ThrowIfNull(something));
+ }
}
}