diff --git a/eng/CodeAnalysis.src.globalconfig b/eng/CodeAnalysis.src.globalconfig
index e5ae41903b5a76..df9d7285695cf8 100644
--- a/eng/CodeAnalysis.src.globalconfig
+++ b/eng/CodeAnalysis.src.globalconfig
@@ -1539,6 +1539,9 @@ dotnet_diagnostic.IDE0160.severity = silent
# IDE0161: Convert to file-scoped namespace
dotnet_diagnostic.IDE0161.severity = silent
+# IDE0190: Null check can be simplified
+dotnet_diagnostic.IDE0190.severity = suggestion
+
# IDE1005: Delegate invocation can be simplified.
dotnet_diagnostic.IDE1005.severity = suggestion
diff --git a/eng/CodeAnalysis.test.globalconfig b/eng/CodeAnalysis.test.globalconfig
index f56a38cfa707c0..6e8b42ea3606c7 100644
--- a/eng/CodeAnalysis.test.globalconfig
+++ b/eng/CodeAnalysis.test.globalconfig
@@ -1536,6 +1536,9 @@ dotnet_diagnostic.IDE0160.severity = silent
# IDE0161: Convert to file-scoped namespace
dotnet_diagnostic.IDE0161.severity = silent
+# IDE0190: Null check can be simplified
+dotnet_diagnostic.IDE0190.severity = silent
+
# IDE1005: Delegate invocation can be simplified.
dotnet_diagnostic.IDE1005.severity = silent
diff --git a/eng/Versions.props b/eng/Versions.props
index 22858083014673..e828e5e89067bd 100644
--- a/eng/Versions.props
+++ b/eng/Versions.props
@@ -20,6 +20,7 @@
false
false
$(AssemblyVersion)
+ true
+ 4.2.0-2.22105.4
2.0.0-alpha.1.21525.11
diff --git a/src/coreclr/System.Private.CoreLib/src/Microsoft/Win32/OAVariantLib.cs b/src/coreclr/System.Private.CoreLib/src/Microsoft/Win32/OAVariantLib.cs
index 3886195966ebd4..958199de5a5d44 100644
--- a/src/coreclr/System.Private.CoreLib/src/Microsoft/Win32/OAVariantLib.cs
+++ b/src/coreclr/System.Private.CoreLib/src/Microsoft/Win32/OAVariantLib.cs
@@ -72,12 +72,8 @@ internal static class OAVariantLib
* Variant and the types that CLR supports explicitly in the
* CLR Variant class.
*/
- internal static Variant ChangeType(Variant source, Type targetClass, short options, CultureInfo culture)
+ internal static Variant ChangeType(Variant source, Type targetClass!!, short options, CultureInfo culture!!)
{
- if (targetClass == null)
- throw new ArgumentNullException(nameof(targetClass));
- if (culture == null)
- throw new ArgumentNullException(nameof(culture));
Variant result = default;
ChangeTypeEx(ref result, ref source,
culture.LCID,
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Attribute.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Attribute.CoreCLR.cs
index 90261791d0c8d5..f33849d2a949e5 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Attribute.CoreCLR.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Attribute.CoreCLR.cs
@@ -450,14 +450,8 @@ public static Attribute[] GetCustomAttributes(MemberInfo element, Type attribute
return GetCustomAttributes(element, attributeType, true);
}
- public static Attribute[] GetCustomAttributes(MemberInfo element, Type attributeType, bool inherit)
+ public static Attribute[] GetCustomAttributes(MemberInfo element!!, Type attributeType!!, bool inherit)
{
- if (element == null)
- throw new ArgumentNullException(nameof(element));
-
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
@@ -474,11 +468,8 @@ public static Attribute[] GetCustomAttributes(MemberInfo element)
return GetCustomAttributes(element, true);
}
- public static Attribute[] GetCustomAttributes(MemberInfo element, bool inherit)
+ public static Attribute[] GetCustomAttributes(MemberInfo element!!, bool inherit)
{
- if (element == null)
- throw new ArgumentNullException(nameof(element));
-
return element.MemberType switch
{
MemberTypes.Property => InternalGetCustomAttributes((PropertyInfo)element, typeof(Attribute), inherit),
@@ -492,15 +483,9 @@ public static bool IsDefined(MemberInfo element, Type attributeType)
return IsDefined(element, attributeType, true);
}
- public static bool IsDefined(MemberInfo element, Type attributeType, bool inherit)
+ public static bool IsDefined(MemberInfo element!!, Type attributeType!!, bool inherit)
{
// Returns true if a custom attribute subclass of attributeType class/interface with inheritance walk
- if (element == null)
- throw new ArgumentNullException(nameof(element));
-
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
@@ -543,14 +528,8 @@ public static Attribute[] GetCustomAttributes(ParameterInfo element, Type attrib
return GetCustomAttributes(element, attributeType, true);
}
- public static Attribute[] GetCustomAttributes(ParameterInfo element, Type attributeType, bool inherit)
+ public static Attribute[] GetCustomAttributes(ParameterInfo element!!, Type attributeType!!, bool inherit)
{
- if (element == null)
- throw new ArgumentNullException(nameof(element));
-
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
@@ -565,11 +544,8 @@ public static Attribute[] GetCustomAttributes(ParameterInfo element, Type attrib
return (element.GetCustomAttributes(attributeType, inherit) as Attribute[])!;
}
- public static Attribute[] GetCustomAttributes(ParameterInfo element, bool inherit)
+ public static Attribute[] GetCustomAttributes(ParameterInfo element!!, bool inherit)
{
- if (element == null)
- throw new ArgumentNullException(nameof(element));
-
if (element.Member == null)
throw new ArgumentException(SR.Argument_InvalidParameterInfo, nameof(element));
@@ -586,14 +562,9 @@ public static bool IsDefined(ParameterInfo element, Type attributeType)
return IsDefined(element, attributeType, true);
}
- public static bool IsDefined(ParameterInfo element, Type attributeType, bool inherit)
+ public static bool IsDefined(ParameterInfo element!!, Type attributeType!!, bool inherit)
{
// Returns true is a custom attribute subclass of attributeType class/interface with inheritance walk
- if (element == null)
- throw new ArgumentNullException(nameof(element));
-
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
@@ -653,22 +624,13 @@ public static Attribute[] GetCustomAttributes(Module element)
return GetCustomAttributes(element, true);
}
- public static Attribute[] GetCustomAttributes(Module element, bool inherit)
+ public static Attribute[] GetCustomAttributes(Module element!!, bool inherit)
{
- if (element == null)
- throw new ArgumentNullException(nameof(element));
-
return (Attribute[])element.GetCustomAttributes(typeof(Attribute), inherit);
}
- public static Attribute[] GetCustomAttributes(Module element, Type attributeType, bool inherit)
+ public static Attribute[] GetCustomAttributes(Module element!!, Type attributeType!!, bool inherit)
{
- if (element == null)
- throw new ArgumentNullException(nameof(element));
-
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
@@ -680,14 +642,9 @@ public static bool IsDefined(Module element, Type attributeType)
return IsDefined(element, attributeType, false);
}
- public static bool IsDefined(Module element, Type attributeType, bool inherit)
+ public static bool IsDefined(Module element!!, Type attributeType!!, bool inherit)
{
// Returns true is a custom attribute subclass of attributeType class/interface with no inheritance walk
- if (element == null)
- throw new ArgumentNullException(nameof(element));
-
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
@@ -723,14 +680,8 @@ public static Attribute[] GetCustomAttributes(Assembly element, Type attributeTy
return GetCustomAttributes(element, attributeType, true);
}
- public static Attribute[] GetCustomAttributes(Assembly element, Type attributeType, bool inherit)
+ public static Attribute[] GetCustomAttributes(Assembly element!!, Type attributeType!!, bool inherit)
{
- if (element == null)
- throw new ArgumentNullException(nameof(element));
-
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
@@ -742,11 +693,8 @@ public static Attribute[] GetCustomAttributes(Assembly element)
return GetCustomAttributes(element, true);
}
- public static Attribute[] GetCustomAttributes(Assembly element, bool inherit)
+ public static Attribute[] GetCustomAttributes(Assembly element!!, bool inherit)
{
- if (element == null)
- throw new ArgumentNullException(nameof(element));
-
return (Attribute[])element.GetCustomAttributes(typeof(Attribute), inherit);
}
@@ -755,14 +703,9 @@ public static bool IsDefined(Assembly element, Type attributeType)
return IsDefined(element, attributeType, true);
}
- public static bool IsDefined(Assembly element, Type attributeType, bool inherit)
+ public static bool IsDefined(Assembly element!!, Type attributeType!!, bool inherit)
{
// Returns true is a custom attribute subclass of attributeType class/interface with no inheritance walk
- if (element == null)
- throw new ArgumentNullException(nameof(element));
-
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Collections/EmptyReadOnlyDictionaryInternal.cs b/src/coreclr/System.Private.CoreLib/src/System/Collections/EmptyReadOnlyDictionaryInternal.cs
index bb9d40bc38db3c..e63e57a3f4a11e 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Collections/EmptyReadOnlyDictionaryInternal.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Collections/EmptyReadOnlyDictionaryInternal.cs
@@ -33,11 +33,8 @@ IEnumerator IEnumerable.GetEnumerator()
// ICollection members
- public void CopyTo(Array array, int index)
+ public void CopyTo(Array array!!, int index)
{
- if (array == null)
- throw new ArgumentNullException(nameof(array));
-
if (array.Rank != 1)
throw new ArgumentException(SR.Arg_RankMultiDimNotSupported);
@@ -58,23 +55,14 @@ public void CopyTo(Array array, int index)
// IDictionary members
- public object? this[object key]
+ public object? this[object key!!]
{
get
{
- if (key == null)
- {
- throw new ArgumentNullException(nameof(key), SR.ArgumentNull_Key);
- }
return null;
}
set
{
- if (key == null)
- {
- throw new ArgumentNullException(nameof(key), SR.ArgumentNull_Key);
- }
-
if (!key.GetType().IsSerializable)
throw new ArgumentException(SR.Argument_NotSerializable, nameof(key));
@@ -94,13 +82,8 @@ public bool Contains(object key)
return false;
}
- public void Add(object key, object? value)
+ public void Add(object key!!, object? value)
{
- if (key == null)
- {
- throw new ArgumentNullException(nameof(key), SR.ArgumentNull_Key);
- }
-
if (!key.GetType().IsSerializable)
throw new ArgumentException(SR.Argument_NotSerializable, nameof(key));
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs
index 23567e475d99b9..3afbff9eb08000 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs
@@ -34,14 +34,8 @@ public abstract partial class Delegate : ICloneable, ISerializable
// This constructor is called from the class generated by the
// compiler generated code
[RequiresUnreferencedCode("The target method might be removed")]
- protected Delegate(object target, string method)
+ protected Delegate(object target!!, string method!!)
{
- if (target == null)
- throw new ArgumentNullException(nameof(target));
-
- if (method == null)
- throw new ArgumentNullException(nameof(method));
-
// This API existed in v1/v1.1 and only expected to create closed
// instance delegates. Constrain the call to BindToMethodName to
// such and don't allow relaxed signature matching (which could make
@@ -57,17 +51,10 @@ protected Delegate(object target, string method)
// This constructor is called from a class to generate a
// delegate based upon a static method name and the Type object
// for the class defining the method.
- protected Delegate([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type target, string method)
+ protected Delegate([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type target!!, string method!!)
{
- if (target == null)
- throw new ArgumentNullException(nameof(target));
-
if (target.ContainsGenericParameters)
throw new ArgumentException(SR.Arg_UnboundGenParam, nameof(target));
-
- if (method == null)
- throw new ArgumentNullException(nameof(method));
-
if (!(target is RuntimeType rtTarget))
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(target));
@@ -220,15 +207,8 @@ protected virtual MethodInfo GetMethodImpl()
// V1 API.
[RequiresUnreferencedCode("The target method might be removed")]
- public static Delegate? CreateDelegate(Type type, object target, string method, bool ignoreCase, bool throwOnBindFailure)
+ public static Delegate? CreateDelegate(Type type!!, object target!!, string method!!, bool ignoreCase, bool throwOnBindFailure)
{
- if (type == null)
- throw new ArgumentNullException(nameof(type));
- if (target == null)
- throw new ArgumentNullException(nameof(target));
- if (method == null)
- throw new ArgumentNullException(nameof(method));
-
if (!(type is RuntimeType rtType))
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type));
if (!rtType.IsDelegate())
@@ -258,17 +238,10 @@ protected virtual MethodInfo GetMethodImpl()
}
// V1 API.
- public static Delegate? CreateDelegate(Type type, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type target, string method, bool ignoreCase, bool throwOnBindFailure)
+ public static Delegate? CreateDelegate(Type type!!, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type target!!, string method!!, bool ignoreCase, bool throwOnBindFailure)
{
- if (type == null)
- throw new ArgumentNullException(nameof(type));
- if (target == null)
- throw new ArgumentNullException(nameof(target));
if (target.ContainsGenericParameters)
throw new ArgumentException(SR.Arg_UnboundGenParam, nameof(target));
- if (method == null)
- throw new ArgumentNullException(nameof(method));
-
if (!(type is RuntimeType rtType))
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type));
if (!(target is RuntimeType rtTarget))
@@ -297,13 +270,9 @@ protected virtual MethodInfo GetMethodImpl()
}
// V1 API.
- public static Delegate? CreateDelegate(Type type, MethodInfo method, bool throwOnBindFailure)
+ public static Delegate? CreateDelegate(Type type!!, MethodInfo method!!, bool throwOnBindFailure)
{
// Validate the parameters.
- if (type == null)
- throw new ArgumentNullException(nameof(type));
- if (method == null)
- throw new ArgumentNullException(nameof(method));
if (!(type is RuntimeType rtType))
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type));
@@ -374,11 +343,9 @@ protected virtual MethodInfo GetMethodImpl()
//
// V2 internal API.
- internal static Delegate CreateDelegateNoSecurityCheck(Type type, object? target, RuntimeMethodHandle method)
+ internal static Delegate CreateDelegateNoSecurityCheck(Type type!!, object? target, RuntimeMethodHandle method)
{
// Validate the parameters.
- if (type == null)
- throw new ArgumentNullException(nameof(type));
if (method.IsNullHandle())
throw new ArgumentNullException(nameof(method));
diff --git a/src/coreclr/System.Private.CoreLib/src/System/GC.cs b/src/coreclr/System.Private.CoreLib/src/System/GC.cs
index 7597672f683164..3e28ba7d3947d5 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/GC.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/GC.cs
@@ -303,10 +303,8 @@ public static void WaitForPendingFinalizers()
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void _SuppressFinalize(object o);
- public static void SuppressFinalize(object obj)
+ public static void SuppressFinalize(object obj!!)
{
- if (obj == null)
- throw new ArgumentNullException(nameof(obj));
_SuppressFinalize(obj);
}
@@ -317,10 +315,8 @@ public static void SuppressFinalize(object obj)
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void _ReRegisterForFinalize(object o);
- public static void ReRegisterForFinalize(object obj)
+ public static void ReRegisterForFinalize(object obj!!)
{
- if (obj == null)
- throw new ArgumentNullException(nameof(obj));
_ReRegisterForFinalize(obj);
}
@@ -609,10 +605,7 @@ internal static void RegisterMemoryLoadChangeNotification(float lowMemoryPercent
{
throw new ArgumentOutOfRangeException(nameof(lowMemoryPercent));
}
- if (notification == null)
- {
- throw new ArgumentNullException(nameof(notification));
- }
+ ArgumentNullException.ThrowIfNull(notification);
lock (s_notifications)
{
@@ -625,13 +618,8 @@ internal static void RegisterMemoryLoadChangeNotification(float lowMemoryPercent
}
}
- internal static void UnregisterMemoryLoadChangeNotification(Action notification)
+ internal static void UnregisterMemoryLoadChangeNotification(Action notification!!)
{
- if (notification == null)
- {
- throw new ArgumentNullException(nameof(notification));
- }
-
lock (s_notifications)
{
for (int i = 0; i < s_notifications.Count; ++i)
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs
index 282d713232b660..651c4aed685f46 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs
@@ -23,11 +23,8 @@ public static Assembly Load(string assemblyString)
[Obsolete("Assembly.LoadWithPartialName has been deprecated. Use Assembly.Load() instead.")]
[System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod
- public static Assembly? LoadWithPartialName(string partialName)
+ public static Assembly? LoadWithPartialName(string partialName!!)
{
- if (partialName == null)
- throw new ArgumentNullException(nameof(partialName));
-
if ((partialName.Length == 0) || (partialName[0] == '\0'))
throw new ArgumentException(SR.Format_StringZeroLength, nameof(partialName));
@@ -45,11 +42,8 @@ public static Assembly Load(string assemblyString)
// Locate an assembly by its name. The name can be strong or
// weak. The assembly is loaded into the domain of the caller.
[System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod
- public static Assembly Load(AssemblyName assemblyRef)
+ public static Assembly Load(AssemblyName assemblyRef!!)
{
- if (assemblyRef == null)
- throw new ArgumentNullException(nameof(assemblyRef));
-
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return RuntimeAssembly.InternalLoad(assemblyRef, ref stackMark, AssemblyLoadContext.CurrentContextualReflectionContext);
}
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs
index e146d3a5853d42..b821a1141fa75a 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs
@@ -57,16 +57,12 @@ internal ModuleBuilder GetModuleBuilder(RuntimeModule module)
#region Constructor
- internal AssemblyBuilder(AssemblyName name,
+ internal AssemblyBuilder(AssemblyName name!!,
AssemblyBuilderAccess access,
Assembly? callingAssembly,
AssemblyLoadContext? assemblyLoadContext,
IEnumerable? unsafeAssemblyAttributes)
{
- if (name == null)
- {
- throw new ArgumentNullException(nameof(name));
- }
if (access != AssemblyBuilderAccess.Run && access != AssemblyBuilderAccess.RunAndCollect)
{
throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, (int)access), nameof(access));
@@ -371,17 +367,8 @@ public override Assembly GetSatelliteAssembly(CultureInfo culture, Version? vers
///
/// Use this function if client decides to form the custom attribute blob themselves.
///
- public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
+ public void SetCustomAttribute(ConstructorInfo con!!, byte[] binaryAttribute!!)
{
- if (con == null)
- {
- throw new ArgumentNullException(nameof(con));
- }
- if (binaryAttribute == null)
- {
- throw new ArgumentNullException(nameof(binaryAttribute));
- }
-
lock (SyncRoot)
{
TypeBuilder.DefineCustomAttribute(
@@ -395,13 +382,8 @@ public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
///
/// Use this function if client wishes to build CustomAttribute using CustomAttributeBuilder.
///
- public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
+ public void SetCustomAttribute(CustomAttributeBuilder customBuilder!!)
{
- if (customBuilder == null)
- {
- throw new ArgumentNullException(nameof(customBuilder));
- }
-
lock (SyncRoot)
{
customBuilder.CreateCustomAttribute(_manifestModuleBuilder, AssemblyBuilderData.AssemblyDefToken);
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/CustomAttributeBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/CustomAttributeBuilder.cs
index 1038d9ba86504a..f910b8a7e7da0d 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/CustomAttributeBuilder.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/CustomAttributeBuilder.cs
@@ -48,20 +48,8 @@ public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, Fi
// public constructor to form the custom attribute with constructor and constructor
// parameters.
- public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, PropertyInfo[] namedProperties, object?[] propertyValues, FieldInfo[] namedFields, object?[] fieldValues)
+ public CustomAttributeBuilder(ConstructorInfo con!!, object?[] constructorArgs!!, PropertyInfo[] namedProperties!!, object?[] propertyValues!!, FieldInfo[] namedFields!!, object?[] fieldValues!!)
{
- if (con == null)
- throw new ArgumentNullException(nameof(con));
- if (constructorArgs == null)
- throw new ArgumentNullException(nameof(constructorArgs));
- if (namedProperties == null)
- throw new ArgumentNullException(nameof(namedProperties));
- if (propertyValues == null)
- throw new ArgumentNullException(nameof(propertyValues));
- if (namedFields == null)
- throw new ArgumentNullException(nameof(namedFields));
- if (fieldValues == null)
- throw new ArgumentNullException(nameof(fieldValues));
#pragma warning disable CA2208 // Instantiate argument exceptions correctly, combination of arguments used
if (namedProperties.Length != propertyValues.Length)
throw new ArgumentException(SR.Arg_ArrayLengthsDiffer, "namedProperties, propertyValues");
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs
index ff832cff7e14a2..d5d36a60b4231f 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs
@@ -32,11 +32,9 @@ internal void GetCallableMethod(RuntimeModule module, DynamicMethod dm)
// *** ILGenerator api ***
- public override LocalBuilder DeclareLocal(Type localType, bool pinned)
+ public override LocalBuilder DeclareLocal(Type localType!!, bool pinned)
{
LocalBuilder localBuilder;
- if (localType == null)
- throw new ArgumentNullException(nameof(localType));
RuntimeType? rtType = localType as RuntimeType;
@@ -55,11 +53,8 @@ public override LocalBuilder DeclareLocal(Type localType, bool pinned)
// Token resolution calls
//
//
- public override void Emit(OpCode opcode, MethodInfo meth)
+ public override void Emit(OpCode opcode, MethodInfo meth!!)
{
- if (meth == null)
- throw new ArgumentNullException(nameof(meth));
-
int stackchange = 0;
int token;
DynamicMethod? dynMeth = meth as DynamicMethod;
@@ -110,11 +105,8 @@ public override void Emit(OpCode opcode, MethodInfo meth)
PutInteger4(token);
}
- public override void Emit(OpCode opcode, ConstructorInfo con)
+ public override void Emit(OpCode opcode, ConstructorInfo con!!)
{
- if (con == null)
- throw new ArgumentNullException(nameof(con));
-
RuntimeConstructorInfo? rtConstructor = con as RuntimeConstructorInfo;
if (rtConstructor == null)
throw new ArgumentException(SR.Argument_MustBeRuntimeMethodInfo, nameof(con));
@@ -137,11 +129,8 @@ public override void Emit(OpCode opcode, ConstructorInfo con)
PutInteger4(token);
}
- public override void Emit(OpCode opcode, Type type)
+ public override void Emit(OpCode opcode, Type type!!)
{
- if (type == null)
- throw new ArgumentNullException(nameof(type));
-
RuntimeType? rtType = type as RuntimeType;
if (rtType == null)
@@ -153,11 +142,8 @@ public override void Emit(OpCode opcode, Type type)
PutInteger4(token);
}
- public override void Emit(OpCode opcode, FieldInfo field)
+ public override void Emit(OpCode opcode, FieldInfo field!!)
{
- if (field == null)
- throw new ArgumentNullException(nameof(field));
-
RuntimeFieldInfo? runtimeField = field as RuntimeFieldInfo;
if (runtimeField == null)
throw new ArgumentException(SR.Argument_MustBeRuntimeFieldInfo, nameof(field));
@@ -173,11 +159,8 @@ public override void Emit(OpCode opcode, FieldInfo field)
PutInteger4(token);
}
- public override void Emit(OpCode opcode, string str)
+ public override void Emit(OpCode opcode, string str!!)
{
- if (str == null)
- throw new ArgumentNullException(nameof(str));
-
int tempVal = GetTokenForString(str);
EnsureCapacity(7);
InternalEmit(opcode);
@@ -264,11 +247,8 @@ public override void EmitCalli(OpCode opcode, CallingConvention unmanagedCallCon
PutInteger4(token);
}
- public override void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[]? optionalParameterTypes)
+ public override void EmitCall(OpCode opcode, MethodInfo methodInfo!!, Type[]? optionalParameterTypes)
{
- if (methodInfo == null)
- throw new ArgumentNullException(nameof(methodInfo));
-
if (!(opcode.Equals(OpCodes.Call) || opcode.Equals(OpCodes.Callvirt) || opcode.Equals(OpCodes.Newobj)))
throw new ArgumentException(SR.Argument_NotMethodCallOpcode, nameof(opcode));
@@ -303,11 +283,8 @@ public override void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[]? opti
PutInteger4(tk);
}
- public override void Emit(OpCode opcode, SignatureHelper signature)
+ public override void Emit(OpCode opcode, SignatureHelper signature!!)
{
- if (signature == null)
- throw new ArgumentNullException(nameof(signature));
-
int stackchange = 0;
EnsureCapacity(7);
InternalEmit(opcode);
@@ -376,8 +353,7 @@ public override void BeginCatchBlock(Type exceptionType)
else
{
// execute this branch if previous clause is Catch or Fault
- if (exceptionType == null)
- throw new ArgumentNullException(nameof(exceptionType));
+ ArgumentNullException.ThrowIfNull(exceptionType);
if (rtType == null)
throw new ArgumentException(SR.Argument_MustBeRuntimeType);
@@ -895,8 +871,8 @@ public unsafe void SetCode(byte* code, int codeSize, int maxStackSize)
{
if (codeSize < 0)
throw new ArgumentOutOfRangeException(nameof(codeSize), SR.ArgumentOutOfRange_GenericPositive);
- if (codeSize > 0 && code == null)
- throw new ArgumentNullException(nameof(code));
+ if (codeSize > 0)
+ ArgumentNullException.ThrowIfNull(code);
m_code = new Span(code, codeSize).ToArray();
m_maxStackSize = maxStackSize;
@@ -913,8 +889,8 @@ public unsafe void SetExceptions(byte* exceptions, int exceptionsSize)
if (exceptionsSize < 0)
throw new ArgumentOutOfRangeException(nameof(exceptionsSize), SR.ArgumentOutOfRange_GenericPositive);
- if (exceptionsSize > 0 && exceptions == null)
- throw new ArgumentNullException(nameof(exceptions));
+ if (exceptionsSize > 0)
+ ArgumentNullException.ThrowIfNull(exceptions);
m_exceptions = new Span(exceptions, exceptionsSize).ToArray();
}
@@ -930,8 +906,8 @@ public unsafe void SetLocalSignature(byte* localSignature, int signatureSize)
if (signatureSize < 0)
throw new ArgumentOutOfRangeException(nameof(signatureSize), SR.ArgumentOutOfRange_GenericPositive);
- if (signatureSize > 0 && localSignature == null)
- throw new ArgumentNullException(nameof(localSignature));
+ if (signatureSize > 0)
+ ArgumentNullException.ThrowIfNull(localSignature);
m_localSignature = new Span(localSignature, signatureSize).ToArray();
}
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs
index b971f07e8f9811..e1819a363d3866 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs
@@ -83,11 +83,8 @@ public DynamicMethod(string name,
public DynamicMethod(string name,
Type? returnType,
Type[]? parameterTypes,
- Module m)
+ Module m!!)
{
- if (m == null)
- throw new ArgumentNullException(nameof(m));
-
Init(name,
MethodAttributes.Public | MethodAttributes.Static,
CallingConventions.Standard,
@@ -102,12 +99,9 @@ public DynamicMethod(string name,
public DynamicMethod(string name,
Type? returnType,
Type[]? parameterTypes,
- Module m,
+ Module m!!,
bool skipVisibility)
{
- if (m == null)
- throw new ArgumentNullException(nameof(m));
-
Init(name,
MethodAttributes.Public | MethodAttributes.Static,
CallingConventions.Standard,
@@ -124,12 +118,9 @@ public DynamicMethod(string name,
CallingConventions callingConvention,
Type? returnType,
Type[]? parameterTypes,
- Module m,
+ Module m!!,
bool skipVisibility)
{
- if (m == null)
- throw new ArgumentNullException(nameof(m));
-
Init(name,
attributes,
callingConvention,
@@ -144,11 +135,8 @@ public DynamicMethod(string name,
public DynamicMethod(string name,
Type? returnType,
Type[]? parameterTypes,
- Type owner)
+ Type owner!!)
{
- if (owner == null)
- throw new ArgumentNullException(nameof(owner));
-
Init(name,
MethodAttributes.Public | MethodAttributes.Static,
CallingConventions.Standard,
@@ -163,12 +151,9 @@ public DynamicMethod(string name,
public DynamicMethod(string name,
Type? returnType,
Type[]? parameterTypes,
- Type owner,
+ Type owner!!,
bool skipVisibility)
{
- if (owner == null)
- throw new ArgumentNullException(nameof(owner));
-
Init(name,
MethodAttributes.Public | MethodAttributes.Static,
CallingConventions.Standard,
@@ -185,12 +170,9 @@ public DynamicMethod(string name,
CallingConventions callingConvention,
Type? returnType,
Type[]? parameterTypes,
- Type owner,
+ Type owner!!,
bool skipVisibility)
{
- if (owner == null)
- throw new ArgumentNullException(nameof(owner));
-
Init(name,
attributes,
callingConvention,
@@ -253,17 +235,17 @@ private static RuntimeModule GetDynamicMethodsModule()
[MemberNotNull(nameof(m_parameterTypes))]
[MemberNotNull(nameof(m_returnType))]
[MemberNotNull(nameof(m_dynMethod))]
- private void Init(string name,
- MethodAttributes attributes,
- CallingConventions callingConvention,
- Type? returnType,
- Type[]? signature,
- Type? owner,
- Module? m,
- bool skipVisibility,
- bool transparentMethod)
+ private void Init(string name!!,
+ MethodAttributes attributes,
+ CallingConventions callingConvention,
+ Type? returnType,
+ Type[]? signature,
+ Type? owner,
+ Module? m,
+ bool skipVisibility,
+ bool transparentMethod)
{
- DynamicMethod.CheckConsistency(attributes, callingConvention);
+ CheckConsistency(attributes, callingConvention);
// check and store the signature
if (signature != null)
@@ -329,10 +311,6 @@ private void Init(string name,
m_ilGenerator = null;
m_fInitLocals = true;
m_methodHandle = null;
-
- if (name == null)
- throw new ArgumentNullException(nameof(name));
-
m_dynMethod = new RTDynamicMethod(this, name, attributes, callingConvention);
}
@@ -655,11 +633,8 @@ public override object[] GetCustomAttributes(bool inherit)
return new object[] { new MethodImplAttribute((MethodImplOptions)GetMethodImplementationFlags()) };
}
- public override bool IsDefined(Type attributeType, bool inherit)
+ public override bool IsDefined(Type attributeType!!, bool inherit)
{
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (attributeType.IsAssignableFrom(typeof(MethodImplAttribute)))
return true;
else
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/EventBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/EventBuilder.cs
index be0ed200842094..c393d8dd1d6857 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/EventBuilder.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/EventBuilder.cs
@@ -46,13 +46,8 @@ internal int GetEventToken()
return m_evToken;
}
- private void SetMethodSemantics(MethodBuilder mdBuilder, MethodSemanticsAttributes semantics)
+ private void SetMethodSemantics(MethodBuilder mdBuilder!!, MethodSemanticsAttributes semantics)
{
- if (mdBuilder == null)
- {
- throw new ArgumentNullException(nameof(mdBuilder));
- }
-
m_type.ThrowIfCreated();
ModuleBuilder module = m_module;
TypeBuilder.DefineMethodSemantics(
@@ -84,12 +79,8 @@ public void AddOtherMethod(MethodBuilder mdBuilder)
// Use this function if client decides to form the custom attribute blob themselves
- public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
+ public void SetCustomAttribute(ConstructorInfo con!!, byte[] binaryAttribute!!)
{
- if (con == null)
- throw new ArgumentNullException(nameof(con));
- if (binaryAttribute == null)
- throw new ArgumentNullException(nameof(binaryAttribute));
m_type.ThrowIfCreated();
TypeBuilder.DefineCustomAttribute(
@@ -100,12 +91,8 @@ public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
}
// Use this function if client wishes to build CustomAttribute using CustomAttributeBuilder
- public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
+ public void SetCustomAttribute(CustomAttributeBuilder customBuilder!!)
{
- if (customBuilder == null)
- {
- throw new ArgumentNullException(nameof(customBuilder));
- }
m_type.ThrowIfCreated();
customBuilder.CreateCustomAttribute(m_module, m_evToken);
}
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/FieldBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/FieldBuilder.cs
index c3e22c5d340719..5c65ff31c00b67 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/FieldBuilder.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/FieldBuilder.cs
@@ -154,14 +154,8 @@ public void SetConstant(object? defaultValue)
TypeBuilder.SetConstantValue(m_typeBuilder.GetModuleBuilder(), m_fieldTok, m_fieldType, defaultValue);
}
- public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
+ public void SetCustomAttribute(ConstructorInfo con!!, byte[] binaryAttribute!!)
{
- if (con == null)
- throw new ArgumentNullException(nameof(con));
-
- if (binaryAttribute == null)
- throw new ArgumentNullException(nameof(binaryAttribute));
-
ModuleBuilder module = (m_typeBuilder.Module as ModuleBuilder)!;
m_typeBuilder.ThrowIfCreated();
@@ -170,11 +164,8 @@ public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
m_fieldTok, module.GetConstructorToken(con), binaryAttribute);
}
- public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
+ public void SetCustomAttribute(CustomAttributeBuilder customBuilder!!)
{
- if (customBuilder == null)
- throw new ArgumentNullException(nameof(customBuilder));
-
m_typeBuilder.ThrowIfCreated();
ModuleBuilder? module = m_typeBuilder.Module as ModuleBuilder;
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ILGenerator.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ILGenerator.cs
index 1b35f5cdbb15f0..65d97cd384e7d3 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ILGenerator.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ILGenerator.cs
@@ -474,11 +474,8 @@ public virtual void Emit(OpCode opcode, int arg)
PutInteger4(arg);
}
- public virtual void Emit(OpCode opcode, MethodInfo meth)
+ public virtual void Emit(OpCode opcode, MethodInfo meth!!)
{
- if (meth == null)
- throw new ArgumentNullException(nameof(meth));
-
if (opcode.Equals(OpCodes.Call) || opcode.Equals(OpCodes.Callvirt) || opcode.Equals(OpCodes.Newobj))
{
EmitCall(opcode, meth, null);
@@ -586,11 +583,8 @@ public virtual void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv
PutInteger4(modBuilder.GetSignatureToken(sig));
}
- public virtual void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[]? optionalParameterTypes)
+ public virtual void EmitCall(OpCode opcode, MethodInfo methodInfo!!, Type[]? optionalParameterTypes)
{
- if (methodInfo == null)
- throw new ArgumentNullException(nameof(methodInfo));
-
if (!(opcode.Equals(OpCodes.Call) || opcode.Equals(OpCodes.Callvirt) || opcode.Equals(OpCodes.Newobj)))
throw new ArgumentException(SR.Argument_NotMethodCallOpcode, nameof(opcode));
@@ -621,11 +615,8 @@ public virtual void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[]? optio
PutInteger4(tk);
}
- public virtual void Emit(OpCode opcode, SignatureHelper signature)
+ public virtual void Emit(OpCode opcode, SignatureHelper signature!!)
{
- if (signature == null)
- throw new ArgumentNullException(nameof(signature));
-
int stackchange = 0;
ModuleBuilder modBuilder = (ModuleBuilder)m_methodBuilder.Module;
int sig = modBuilder.GetSignatureToken(signature);
@@ -655,11 +646,8 @@ public virtual void Emit(OpCode opcode, SignatureHelper signature)
PutInteger4(tempVal);
}
- public virtual void Emit(OpCode opcode, ConstructorInfo con)
+ public virtual void Emit(OpCode opcode, ConstructorInfo con!!)
{
- if (con == null)
- throw new ArgumentNullException(nameof(con));
-
int stackchange = 0;
// Constructors cannot be generic so the value of UseMethodDef doesn't matter.
@@ -772,11 +760,8 @@ public virtual void Emit(OpCode opcode, Label label)
}
}
- public virtual void Emit(OpCode opcode, Label[] labels)
+ public virtual void Emit(OpCode opcode, Label[] labels!!)
{
- if (labels == null)
- throw new ArgumentNullException(nameof(labels));
-
// Emitting a switch table
int i;
@@ -818,14 +803,9 @@ public virtual void Emit(OpCode opcode, string str)
PutInteger4(tempVal);
}
- public virtual void Emit(OpCode opcode, LocalBuilder local)
+ public virtual void Emit(OpCode opcode, LocalBuilder local!!)
{
// Puts the opcode onto the IL stream followed by the information for local variable local.
-
- if (local == null)
- {
- throw new ArgumentNullException(nameof(local));
- }
int tempVal = local.GetLocalIndex();
if (local.GetMethodBuilder() != m_methodBuilder)
{
@@ -1022,10 +1002,7 @@ public virtual void BeginCatchBlock(Type exceptionType)
else
{
// execute this branch if previous clause is Catch or Fault
- if (exceptionType == null)
- {
- throw new ArgumentNullException(nameof(exceptionType));
- }
+ ArgumentNullException.ThrowIfNull(exceptionType);
Emit(OpCodes.Leave, current.GetEndLabel());
}
@@ -1188,18 +1165,13 @@ public virtual void EmitWriteLine(LocalBuilder localBuilder)
Emit(OpCodes.Callvirt, mi);
}
- public virtual void EmitWriteLine(FieldInfo fld)
+ public virtual void EmitWriteLine(FieldInfo fld!!)
{
// Emits the IL necessary to call WriteLine with fld. It is
// an error to call EmitWriteLine with a fld which is not of
// one of the types for which Console.WriteLine implements overloads. (e.g.
// we do *not* call ToString on the fields.
- if (fld == null)
- {
- throw new ArgumentNullException(nameof(fld));
- }
-
Type consoleType = Type.GetType(ConsoleTypeFullName, throwOnError: true)!;
MethodInfo prop = consoleType.GetMethod("get_Out")!;
Emit(OpCodes.Call, prop);
@@ -1252,10 +1224,7 @@ public virtual LocalBuilder DeclareLocal(Type localType, bool pinned)
throw new InvalidOperationException(SR.InvalidOperation_TypeHasBeenCreated);
}
- if (localType == null)
- {
- throw new ArgumentNullException(nameof(localType));
- }
+ ArgumentNullException.ThrowIfNull(localType);
if (methodBuilder.m_bIsBaked)
{
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs
index 9d11aacaf53ea8..8c1c738f0d390b 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs
@@ -130,14 +130,10 @@ internal MethodBuilder(string name, MethodAttributes attributes, CallingConventi
#region Internal Members
- internal void CreateMethodBodyHelper(ILGenerator il)
+ internal void CreateMethodBodyHelper(ILGenerator il!!)
{
// Sets the IL of the method. An ILGenerator is passed as an argument and the method
// queries this instance to get all of the information which it needs.
- if (il == null)
- {
- throw new ArgumentNullException(nameof(il));
- }
__ExceptionInfo[] excp;
int counter = 0;
@@ -517,11 +513,8 @@ public override MethodInfo MakeGenericMethod(params Type[] typeArguments)
return MethodBuilderInstantiation.MakeGenericMethod(this, typeArguments);
}
- public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names)
+ public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names!!)
{
- if (names == null)
- throw new ArgumentNullException(nameof(names));
-
if (names.Length == 0)
throw new ArgumentException(SR.Arg_EmptyArray, nameof(names));
@@ -529,8 +522,7 @@ public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] nam
throw new InvalidOperationException(SR.InvalidOperation_GenericParametersAlreadySet);
for (int i = 0; i < names.Length; i++)
- if (names[i] == null)
- throw new ArgumentNullException(nameof(names));
+ ArgumentNullException.ThrowIfNull(names[i], nameof(names));
if (m_token != 0)
throw new InvalidOperationException(SR.InvalidOperation_MethodBuilderBaked);
@@ -734,13 +726,8 @@ internal Module GetModule()
return GetModuleBuilder();
}
- public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
+ public void SetCustomAttribute(ConstructorInfo con!!, byte[] binaryAttribute!!)
{
- if (con is null)
- throw new ArgumentNullException(nameof(con));
- if (binaryAttribute is null)
- throw new ArgumentNullException(nameof(binaryAttribute));
-
ThrowIfGeneric();
TypeBuilder.DefineCustomAttribute(m_module, MetadataToken,
@@ -751,11 +738,8 @@ public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
ParseCA(con);
}
- public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
+ public void SetCustomAttribute(CustomAttributeBuilder customBuilder!!)
{
- if (customBuilder == null)
- throw new ArgumentNullException(nameof(customBuilder));
-
ThrowIfGeneric();
customBuilder.CreateCustomAttribute((ModuleBuilder)m_module, MetadataToken);
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.cs
index f1ffa97874925f..3bf175e13e1f62 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.cs
@@ -216,14 +216,9 @@ private int GetTypeRefNested(Type type, Module? refedModule, string? strRefedMod
return GetTypeRef(new QCallModule(ref thisModule), typeName, new QCallModule(ref refedRuntimeModule), strRefedModuleFileName, tkResolution);
}
- internal int InternalGetConstructorToken(ConstructorInfo con, bool usingRef)
+ internal int InternalGetConstructorToken(ConstructorInfo con!!, bool usingRef)
{
// Helper to get constructor token. If usingRef is true, we will never use the def token
- if (con == null)
- {
- throw new ArgumentNullException(nameof(con));
- }
-
int tr;
int mr;
@@ -1033,13 +1028,8 @@ internal int GetTypeToken(Type type)
return GetTypeTokenInternal(type, getGenericDefinition: true);
}
- private int GetTypeTokenWorkerNoLock(Type type, bool getGenericDefinition)
+ private int GetTypeTokenWorkerNoLock(Type type!!, bool getGenericDefinition)
{
- if (type == null)
- {
- throw new ArgumentNullException(nameof(type));
- }
-
AssemblyBuilder.CheckContext(type);
// Return a token for the class relative to the Module. Tokens
@@ -1131,15 +1121,10 @@ internal int GetMethodToken(MethodInfo method)
// 1. GetMethodToken
// 2. ldtoken (see ILGenerator)
// For all other occasions we should return the method on the generic type instantiated on the formal parameters.
- private int GetMethodTokenNoLock(MethodInfo method, bool getGenericTypeDefinition)
+ private int GetMethodTokenNoLock(MethodInfo method!!, bool getGenericTypeDefinition)
{
// Return a MemberRef token if MethodInfo is not defined in this module. Or
// return the MethodDef token.
- if (method == null)
- {
- throw new ArgumentNullException(nameof(method));
- }
-
int tr;
int mr;
@@ -1370,13 +1355,8 @@ internal int GetFieldToken(FieldInfo field)
}
}
- private int GetFieldTokenNoLock(FieldInfo field)
+ private int GetFieldTokenNoLock(FieldInfo field!!)
{
- if (field == null)
- {
- throw new ArgumentNullException(nameof(field));
- }
-
int tr;
int mr;
@@ -1448,41 +1428,26 @@ private int GetFieldTokenNoLock(FieldInfo field)
return mr;
}
- internal int GetStringConstant(string str)
+ internal int GetStringConstant(string str!!)
{
- if (str == null)
- {
- throw new ArgumentNullException(nameof(str));
- }
-
// Returns a token representing a String constant. If the string
// value has already been defined, the existing token will be returned.
ModuleBuilder thisModule = this;
return GetStringConstant(new QCallModule(ref thisModule), str, str.Length);
}
- internal int GetSignatureToken(SignatureHelper sigHelper)
+ internal int GetSignatureToken(SignatureHelper sigHelper!!)
{
// Define signature token given a signature helper. This will define a metadata
// token for the signature described by SignatureHelper.
- if (sigHelper == null)
- {
- throw new ArgumentNullException(nameof(sigHelper));
- }
-
- // get the signature in byte form
+ // Get the signature in byte form.
byte[] sigBytes = sigHelper.InternalGetSignature(out int sigLength);
ModuleBuilder thisModule = this;
return TypeBuilder.GetTokenFromSig(new QCallModule(ref thisModule), sigBytes, sigLength);
}
- internal int GetSignatureToken(byte[] sigBytes, int sigLength)
+ internal int GetSignatureToken(byte[] sigBytes!!, int sigLength)
{
- if (sigBytes == null)
- {
- throw new ArgumentNullException(nameof(sigBytes));
- }
-
byte[] localSigBytes = new byte[sigBytes.Length];
Buffer.BlockCopy(sigBytes, 0, localSigBytes, 0, sigBytes.Length);
@@ -1494,17 +1459,8 @@ internal int GetSignatureToken(byte[] sigBytes, int sigLength)
#region Other
- public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
+ public void SetCustomAttribute(ConstructorInfo con!!, byte[] binaryAttribute!!)
{
- if (con == null)
- {
- throw new ArgumentNullException(nameof(con));
- }
- if (binaryAttribute == null)
- {
- throw new ArgumentNullException(nameof(binaryAttribute));
- }
-
TypeBuilder.DefineCustomAttribute(
this,
1, // This is hard coding the module token to 1
@@ -1512,13 +1468,8 @@ public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
binaryAttribute);
}
- public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
+ public void SetCustomAttribute(CustomAttributeBuilder customBuilder!!)
{
- if (customBuilder == null)
- {
- throw new ArgumentNullException(nameof(customBuilder));
- }
-
customBuilder.CreateCustomAttribute(this, 1); // This is hard coding the module token to 1
}
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ParameterBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ParameterBuilder.cs
index 79379c535874c7..6be9a88cfe9210 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ParameterBuilder.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ParameterBuilder.cs
@@ -18,17 +18,8 @@ public virtual void SetConstant(object? defaultValue)
}
// Use this function if client decides to form the custom attribute blob themselves
- public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
+ public void SetCustomAttribute(ConstructorInfo con!!, byte[] binaryAttribute!!)
{
- if (con == null)
- {
- throw new ArgumentNullException(nameof(con));
- }
- if (binaryAttribute == null)
- {
- throw new ArgumentNullException(nameof(binaryAttribute));
- }
-
TypeBuilder.DefineCustomAttribute(
_methodBuilder.GetModuleBuilder(),
_token,
@@ -37,12 +28,8 @@ public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
}
// Use this function if client wishes to build CustomAttribute using CustomAttributeBuilder
- public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
+ public void SetCustomAttribute(CustomAttributeBuilder customBuilder!!)
{
- if (customBuilder == null)
- {
- throw new ArgumentNullException(nameof(customBuilder));
- }
customBuilder.CreateCustomAttribute((ModuleBuilder)(_methodBuilder.GetModule()), _token);
}
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/PropertyBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/PropertyBuilder.cs
index 89accc39caeb98..71dfd932e12605 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/PropertyBuilder.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/PropertyBuilder.cs
@@ -67,13 +67,8 @@ public void SetConstant(object? defaultValue)
public override Module Module => m_containingType.Module;
- private void SetMethodSemantics(MethodBuilder mdBuilder, MethodSemanticsAttributes semantics)
+ private void SetMethodSemantics(MethodBuilder mdBuilder!!, MethodSemanticsAttributes semantics)
{
- if (mdBuilder == null)
- {
- throw new ArgumentNullException(nameof(mdBuilder));
- }
-
m_containingType.ThrowIfCreated();
ModuleBuilder module = m_moduleBuilder;
TypeBuilder.DefineMethodSemantics(
@@ -102,13 +97,8 @@ public void AddOtherMethod(MethodBuilder mdBuilder)
// Use this function if client decides to form the custom attribute blob themselves
- public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
+ public void SetCustomAttribute(ConstructorInfo con!!, byte[] binaryAttribute!!)
{
- if (con == null)
- throw new ArgumentNullException(nameof(con));
- if (binaryAttribute == null)
- throw new ArgumentNullException(nameof(binaryAttribute));
-
m_containingType.ThrowIfCreated();
TypeBuilder.DefineCustomAttribute(
m_moduleBuilder,
@@ -118,12 +108,8 @@ public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
}
// Use this function if client wishes to build CustomAttribute using CustomAttributeBuilder
- public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
+ public void SetCustomAttribute(CustomAttributeBuilder customBuilder!!)
{
- if (customBuilder == null)
- {
- throw new ArgumentNullException(nameof(customBuilder));
- }
m_containingType.ThrowIfCreated();
customBuilder.CreateCustomAttribute(m_moduleBuilder, m_tkProperty);
}
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs
index 9dd3a2fbb63e89..cacb7ac0121c55 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs
@@ -167,14 +167,8 @@ public static SignatureHelper GetPropertySigHelper(Module? mod, CallingConventio
return sigHelp;
}
- internal static SignatureHelper GetTypeSigToken(Module module, Type type)
+ internal static SignatureHelper GetTypeSigToken(Module module!!, Type type!!)
{
- if (module == null)
- throw new ArgumentNullException(nameof(module));
-
- if (type == null)
- throw new ArgumentNullException(nameof(type));
-
return new SignatureHelper(module, type);
}
#endregion
@@ -308,8 +302,7 @@ private void AddOneArgTypeHelper(Type clsArgument, Type[]? requiredCustomModifie
{
Type t = requiredCustomModifiers[i];
- if (t == null)
- throw new ArgumentNullException(nameof(requiredCustomModifiers));
+ ArgumentNullException.ThrowIfNull(t, nameof(requiredCustomModifiers));
if (t.HasElementType)
throw new ArgumentException(SR.Argument_ArraysInvalid, nameof(requiredCustomModifiers));
@@ -746,11 +739,8 @@ public void AddArgument(Type clsArgument)
AddArgument(clsArgument, null, null);
}
- public void AddArgument(Type argument, bool pinned)
+ public void AddArgument(Type argument!!, bool pinned)
{
- if (argument == null)
- throw new ArgumentNullException(nameof(argument));
-
IncrementArgCounts();
AddOneArgTypeHelper(argument, pinned);
}
@@ -777,8 +767,7 @@ public void AddArgument(Type argument, Type[]? requiredCustomModifiers, Type[]?
if (m_sigDone)
throw new ArgumentException(SR.Argument_SigIsFinalized);
- if (argument == null)
- throw new ArgumentNullException(nameof(argument));
+ ArgumentNullException.ThrowIfNull(argument);
IncrementArgCounts();
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs
index f714e5ebd59975..735956362086dd 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs
@@ -224,11 +224,8 @@ internal SymbolType(TypeKind typeKind)
#endregion
#region Internal Members
- internal void SetElementType(Type baseType)
+ internal void SetElementType(Type baseType!!)
{
- if (baseType is null)
- throw new ArgumentNullException(nameof(baseType));
-
m_baseType = baseType;
}
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs
index ad80acad078350..0f353d383e0afb 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs
@@ -25,23 +25,14 @@ private sealed class CustAttr
private readonly byte[]? m_binaryAttribute;
private readonly CustomAttributeBuilder? m_customBuilder;
- public CustAttr(ConstructorInfo con, byte[] binaryAttribute)
+ public CustAttr(ConstructorInfo con!!, byte[] binaryAttribute!!)
{
- if (con is null)
- throw new ArgumentNullException(nameof(con));
-
- if (binaryAttribute is null)
- throw new ArgumentNullException(nameof(binaryAttribute));
-
m_con = con;
m_binaryAttribute = binaryAttribute;
}
- public CustAttr(CustomAttributeBuilder customBuilder)
+ public CustAttr(CustomAttributeBuilder customBuilder!!)
{
- if (customBuilder is null)
- throw new ArgumentNullException(nameof(customBuilder));
-
m_customBuilder = customBuilder;
}
@@ -509,11 +500,8 @@ internal TypeBuilder(
{
for (i = 0; i < interfaces.Length; i++)
{
- if (interfaces[i] == null)
- {
- // cannot contain null in the interface list
- throw new ArgumentNullException(nameof(interfaces));
- }
+ // cannot contain null in the interface list
+ ArgumentNullException.ThrowIfNull(interfaces[i], nameof(interfaces));
}
interfaceTokens = new int[interfaces.Length + 1];
for (i = 0; i < interfaces.Length; i++)
@@ -1120,8 +1108,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
if (!IsCreated())
throw new NotSupportedException(SR.NotSupported_TypeNotYetCreated);
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
+ ArgumentNullException.ThrowIfNull(attributeType);
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
@@ -1134,8 +1121,7 @@ public override bool IsDefined(Type attributeType, bool inherit)
if (!IsCreated())
throw new NotSupportedException(SR.NotSupported_TypeNotYetCreated);
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
+ ArgumentNullException.ThrowIfNull(attributeType);
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
@@ -1161,17 +1147,13 @@ internal void SetInterfaces(params Type[]? interfaces)
}
}
- public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names)
+ public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names!!)
{
- if (names == null)
- throw new ArgumentNullException(nameof(names));
-
if (names.Length == 0)
throw new ArgumentException(SR.Arg_EmptyArray, nameof(names));
for (int i = 0; i < names.Length; i++)
- if (names[i] == null)
- throw new ArgumentNullException(nameof(names));
+ ArgumentNullException.ThrowIfNull(names[i], nameof(names));
if (m_inst != null)
throw new InvalidOperationException();
@@ -1214,14 +1196,8 @@ public void DefineMethodOverride(MethodInfo methodInfoBody, MethodInfo methodInf
}
}
- private void DefineMethodOverrideNoLock(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration)
+ private void DefineMethodOverrideNoLock(MethodInfo methodInfoBody!!, MethodInfo methodInfoDeclaration!!)
{
- if (methodInfoBody == null)
- throw new ArgumentNullException(nameof(methodInfoBody));
-
- if (methodInfoDeclaration == null)
- throw new ArgumentNullException(nameof(methodInfoDeclaration));
-
ThrowIfCreated();
if (!ReferenceEquals(methodInfoBody.DeclaringType, this))
@@ -1700,11 +1676,8 @@ public FieldBuilder DefineInitializedData(string name, byte[] data, FieldAttribu
}
}
- private FieldBuilder DefineInitializedDataNoLock(string name, byte[] data, FieldAttributes attributes)
+ private FieldBuilder DefineInitializedDataNoLock(string name, byte[] data!!, FieldAttributes attributes)
{
- if (data == null)
- throw new ArgumentNullException(nameof(data));
-
// This method will define an initialized Data in .sdata.
// We will create a fake TypeDef to represent the data with size. This TypeDef
// will be the signature for the Field.
@@ -2096,13 +2069,8 @@ public void SetParent([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes
}
}
- public void AddInterfaceImplementation([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType)
+ public void AddInterfaceImplementation([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType!!)
{
- if (interfaceType == null)
- {
- throw new ArgumentNullException(nameof(interfaceType));
- }
-
AssemblyBuilder.CheckContext(interfaceType);
ThrowIfCreated();
@@ -2125,23 +2093,14 @@ internal int TypeToken
}
}
- public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
+ public void SetCustomAttribute(ConstructorInfo con!!, byte[] binaryAttribute!!)
{
- if (con == null)
- throw new ArgumentNullException(nameof(con));
-
- if (binaryAttribute == null)
- throw new ArgumentNullException(nameof(binaryAttribute));
-
DefineCustomAttribute(m_module, m_tdType, ((ModuleBuilder)m_module).GetConstructorToken(con),
binaryAttribute);
}
- public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
+ public void SetCustomAttribute(CustomAttributeBuilder customBuilder!!)
{
- if (customBuilder == null)
- throw new ArgumentNullException(nameof(customBuilder));
-
customBuilder.CreateCustomAttribute((ModuleBuilder)m_module, m_tdType);
}
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs
index 3f2cadc852e6af..4e195408633ece 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs
@@ -24,13 +24,11 @@ internal static Type MakeGenericType(Type type, Type[] typeArguments)
if (!type.IsGenericTypeDefinition)
throw new InvalidOperationException();
- if (typeArguments == null)
- throw new ArgumentNullException(nameof(typeArguments));
+ ArgumentNullException.ThrowIfNull(typeArguments);
foreach (Type t in typeArguments)
{
- if (t == null)
- throw new ArgumentNullException(nameof(typeArguments));
+ ArgumentNullException.ThrowIfNull(t, nameof(typeArguments));
}
return new TypeBuilderInstantiation(type, typeArguments);
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/MemberInfo.Internal.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/MemberInfo.Internal.cs
index 7d5736ee88de11..fe79a0e8e7c83f 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/MemberInfo.Internal.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/MemberInfo.Internal.cs
@@ -7,11 +7,8 @@ public abstract partial class MemberInfo
{
internal virtual bool CacheEquals(object? o) { throw new NotImplementedException(); }
- internal bool HasSameMetadataDefinitionAsCore(MemberInfo other) where TOther : MemberInfo
+ internal bool HasSameMetadataDefinitionAsCore(MemberInfo other!!) where TOther : MemberInfo
{
- if (other is null)
- throw new ArgumentNullException(nameof(other));
-
// Ensure that "other" is a runtime-implemented MemberInfo. Do this check before calling any methods on it!
if (!(other is TOther))
return false;
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Metadata/AssemblyExtensions.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Metadata/AssemblyExtensions.cs
index 332dd6301cde09..fa7385a02dfc66 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Metadata/AssemblyExtensions.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Metadata/AssemblyExtensions.cs
@@ -28,13 +28,8 @@ public static partial class AssemblyExtensions
/// The caller is responsible for keeping the assembly object alive while accessing the metadata blob.
///
[CLSCompliant(false)] // out byte* blob
- public static unsafe bool TryGetRawMetadata(this Assembly assembly, out byte* blob, out int length)
+ public static unsafe bool TryGetRawMetadata(this Assembly assembly!!, out byte* blob, out int length)
{
- if (assembly == null)
- {
- throw new ArgumentNullException(nameof(assembly));
- }
-
blob = null;
length = 0;
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Metadata/MetadataUpdater.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Metadata/MetadataUpdater.cs
index 87366de57eb010..462df732af729c 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Metadata/MetadataUpdater.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Metadata/MetadataUpdater.cs
@@ -36,7 +36,7 @@ public static void ApplyUpdate(Assembly assembly, ReadOnlySpan metadataDel
{
if (assembly is not RuntimeAssembly runtimeAssembly)
{
- if (assembly is null) throw new ArgumentNullException(nameof(assembly));
+ ArgumentNullException.ThrowIfNull(assembly);
throw new ArgumentException(SR.Argument_MustBeRuntimeAssembly);
}
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs
index 11a0aa3afa1a71..1b66fb3c8244c1 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs
@@ -184,12 +184,10 @@ private static partial void GetType(QCallAssembly assembly,
ObjectHandleOnStack assemblyLoadContext);
[RequiresUnreferencedCode("Types might be removed")]
- public override Type? GetType(string name, bool throwOnError, bool ignoreCase)
+ public override Type? GetType(
+ string name!!, // throw on null strings regardless of the value of "throwOnError"
+ bool throwOnError, bool ignoreCase)
{
- // throw on null strings regardless of the value of "throwOnError"
- if (name == null)
- throw new ArgumentNullException(nameof(name));
-
RuntimeType? type = null;
object? keepAlive = null;
AssemblyLoadContext? assemblyLoadContextStack = AssemblyLoadContext.CurrentContextualReflectionContext;
@@ -262,8 +260,8 @@ public override bool IsCollectible
// Load a resource based on the NameSpace of the type.
public override Stream? GetManifestResourceStream(Type type, string name)
{
- if (type == null && name == null)
- throw new ArgumentNullException(nameof(type));
+ if (name == null)
+ ArgumentNullException.ThrowIfNull(type);
string? nameSpace = type?.Namespace;
@@ -304,22 +302,16 @@ public override object[] GetCustomAttributes(bool inherit)
return CustomAttribute.GetCustomAttributes(this, (typeof(object) as RuntimeType)!);
}
- public override object[] GetCustomAttributes(Type attributeType, bool inherit)
+ public override object[] GetCustomAttributes(Type attributeType!!, bool inherit)
{
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
}
- public override bool IsDefined(Type attributeType, bool inherit)
+ public override bool IsDefined(Type attributeType!!, bool inherit)
{
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
@@ -576,11 +568,8 @@ public override Assembly GetSatelliteAssembly(CultureInfo culture)
}
// Useful for binding to a very specific version of a satellite assembly
- public override Assembly GetSatelliteAssembly(CultureInfo culture, Version? version)
+ public override Assembly GetSatelliteAssembly(CultureInfo culture!!, Version? version)
{
- if (culture == null)
- throw new ArgumentNullException(nameof(culture));
-
return InternalGetSatelliteAssembly(culture, version, throwOnFileNotFound: true)!;
}
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeConstructorInfo.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeConstructorInfo.CoreCLR.cs
index 7f34d5438c6bea..267b738794a63a 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeConstructorInfo.CoreCLR.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeConstructorInfo.CoreCLR.cs
@@ -115,22 +115,16 @@ public override object[] GetCustomAttributes(bool inherit)
return CustomAttribute.GetCustomAttributes(this, (typeof(object) as RuntimeType)!);
}
- public override object[] GetCustomAttributes(Type attributeType, bool inherit)
+ public override object[] GetCustomAttributes(Type attributeType!!, bool inherit)
{
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
}
- public override bool IsDefined(Type attributeType, bool inherit)
+ public override bool IsDefined(Type attributeType!!, bool inherit)
{
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs
index edf7aeb269c26b..b6ab1bf4e45fa9 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs
@@ -679,11 +679,8 @@ private static extern void ParseAttributeArguments(
internal static void ParseAttributeArguments(ConstArray attributeBlob,
ref CustomAttributeCtorParameter[] customAttributeCtorParameters,
ref CustomAttributeNamedParameter[] customAttributeNamedParameters,
- RuntimeModule customAttributeModule)
+ RuntimeModule customAttributeModule!!)
{
- if (customAttributeModule is null)
- throw new ArgumentNullException(nameof(customAttributeModule));
-
Debug.Assert(customAttributeCtorParameters is not null);
Debug.Assert(customAttributeNamedParameters is not null);
@@ -713,11 +710,8 @@ internal readonly struct CustomAttributeNamedParameter
private readonly CustomAttributeType m_type;
private readonly CustomAttributeEncodedArgument m_encodedArgument;
- public CustomAttributeNamedParameter(string argumentName, CustomAttributeEncoding fieldOrProperty, CustomAttributeType type)
+ public CustomAttributeNamedParameter(string argumentName!!, CustomAttributeEncoding fieldOrProperty, CustomAttributeType type)
{
- if (argumentName is null)
- throw new ArgumentNullException(nameof(argumentName));
-
m_argumentName = argumentName;
m_fieldOrProperty = fieldOrProperty;
m_padding = fieldOrProperty;
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeEventInfo.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeEventInfo.cs
index 6abf767636e0dd..b0d5805804208c 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeEventInfo.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeEventInfo.cs
@@ -76,22 +76,16 @@ public override object[] GetCustomAttributes(bool inherit)
return CustomAttribute.GetCustomAttributes(this, (typeof(object) as RuntimeType)!);
}
- public override object[] GetCustomAttributes(Type attributeType, bool inherit)
+ public override object[] GetCustomAttributes(Type attributeType!!, bool inherit)
{
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
}
- public override bool IsDefined(Type attributeType, bool inherit)
+ public override bool IsDefined(Type attributeType!!, bool inherit)
{
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeFieldInfo.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeFieldInfo.cs
index 10605ef5f533c1..d2ddb1f36091c1 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeFieldInfo.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeFieldInfo.cs
@@ -61,22 +61,16 @@ public override object[] GetCustomAttributes(bool inherit)
return CustomAttribute.GetCustomAttributes(this, (typeof(object) as RuntimeType)!);
}
- public override object[] GetCustomAttributes(Type attributeType, bool inherit)
+ public override object[] GetCustomAttributes(Type attributeType!!, bool inherit)
{
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
}
- public override bool IsDefined(Type attributeType, bool inherit)
+ public override bool IsDefined(Type attributeType!!, bool inherit)
{
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.CoreCLR.cs
index 43984c60a5468d..2b26b90cb7770f 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.CoreCLR.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.CoreCLR.cs
@@ -205,22 +205,16 @@ public override object[] GetCustomAttributes(bool inherit)
return CustomAttribute.GetCustomAttributes(this, (typeof(object) as RuntimeType)!, inherit);
}
- public override object[] GetCustomAttributes(Type attributeType, bool inherit)
+ public override object[] GetCustomAttributes(Type attributeType!!, bool inherit)
{
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType, inherit);
}
- public override bool IsDefined(Type attributeType, bool inherit)
+ public override bool IsDefined(Type attributeType!!, bool inherit)
{
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
@@ -417,12 +411,9 @@ public override Delegate CreateDelegate(Type delegateType, object? target)
DelegateBindingFlags.RelaxedSignature);
}
- private Delegate CreateDelegateInternal(Type delegateType, object? firstArgument, DelegateBindingFlags bindingFlags)
+ private Delegate CreateDelegateInternal(Type delegateType!!, object? firstArgument, DelegateBindingFlags bindingFlags)
{
// Validate the parameters.
- if (delegateType == null)
- throw new ArgumentNullException(nameof(delegateType));
-
RuntimeType? rtType = delegateType as RuntimeType;
if (rtType == null)
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(delegateType));
@@ -443,11 +434,8 @@ private Delegate CreateDelegateInternal(Type delegateType, object? firstArgument
#region Generics
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
- public override MethodInfo MakeGenericMethod(params Type[] methodInstantiation)
+ public override MethodInfo MakeGenericMethod(params Type[] methodInstantiation!!)
{
- if (methodInstantiation == null)
- throw new ArgumentNullException(nameof(methodInstantiation));
-
RuntimeType[] methodInstantionRuntimeType = new RuntimeType[methodInstantiation.Length];
if (!IsGenericMethodDefinition)
@@ -457,9 +445,7 @@ public override MethodInfo MakeGenericMethod(params Type[] methodInstantiation)
for (int i = 0; i < methodInstantiation.Length; i++)
{
Type methodInstantiationElem = methodInstantiation[i];
-
- if (methodInstantiationElem == null)
- throw new ArgumentNullException();
+ ArgumentNullException.ThrowIfNull(methodInstantiationElem, null);
RuntimeType? rtMethodInstantiationElem = methodInstantiationElem as RuntimeType;
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs
index e8b52d01d5e0da..4b5760f14c713e 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs
@@ -385,22 +385,16 @@ public override object[] GetCustomAttributes(bool inherit)
return CustomAttribute.GetCustomAttributes(this, (typeof(object) as RuntimeType)!);
}
- public override object[] GetCustomAttributes(Type attributeType, bool inherit)
+ public override object[] GetCustomAttributes(Type attributeType!!, bool inherit)
{
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
}
- public override bool IsDefined(Type attributeType, bool inherit)
+ public override bool IsDefined(Type attributeType!!, bool inherit)
{
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
@@ -420,12 +414,10 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont
}
[RequiresUnreferencedCode("Types might be removed")]
- public override Type? GetType(string className, bool throwOnError, bool ignoreCase)
+ public override Type? GetType(
+ string className!!, // throw on null strings regardless of the value of "throwOnError"
+ bool throwOnError, bool ignoreCase)
{
- // throw on null strings regardless of the value of "throwOnError"
- if (className == null)
- throw new ArgumentNullException(nameof(className));
-
RuntimeType? retType = null;
object? keepAlive = null;
RuntimeModule thisAsLocal = this;
@@ -483,15 +475,9 @@ public override FieldInfo[] GetFields(BindingFlags bindingFlags)
}
[RequiresUnreferencedCode("Fields might be removed")]
- public override FieldInfo? GetField(string name, BindingFlags bindingAttr)
+ public override FieldInfo? GetField(string name!!, BindingFlags bindingAttr)
{
- if (name == null)
- throw new ArgumentNullException(nameof(name));
-
- if (RuntimeType == null)
- return null;
-
- return RuntimeType.GetField(name, bindingAttr);
+ return RuntimeType?.GetField(name, bindingAttr);
}
[RequiresUnreferencedCode("Methods might be removed")]
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs
index 173caf6855a0aa..33777c745dbfcb 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs
@@ -507,11 +507,8 @@ public override object[] GetCustomAttributes(bool inherit)
return CustomAttribute.GetCustomAttributes(this, (typeof(object) as RuntimeType)!);
}
- public override object[] GetCustomAttributes(Type attributeType, bool inherit)
+ public override object[] GetCustomAttributes(Type attributeType!!, bool inherit)
{
- if (attributeType == null)
- throw new ArgumentNullException(nameof(attributeType));
-
if (MdToken.IsNullToken(m_tkParamDef))
return Array.Empty