diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs index c08fa0c44ea..24b9332bde5 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs @@ -149,8 +149,10 @@ public void RegisterCimIndication( uint operationTimeout) { DebugHelper.WriteLogEx("queryDialect = '{0}'; queryExpression = '{1}'", 0, queryDialect, queryExpression); - - ArgumentNullException.ThrowIfNull(cimSession, string.Format(CultureInfo.CurrentUICulture, CimCmdletStrings.NullArgument, nameof(cimSession))); + if (cimSession == null) + { + throw new ArgumentNullException(string.Format(CultureInfo.CurrentUICulture, CimCmdletStrings.NullArgument, nameof(cimSession))); + } this.TargetComputerName = cimSession.ComputerName; CimSessionProxy proxy = CreateSessionProxy(cimSession, operationTimeout); diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs index adcab254231..0cb0cfa7cb1 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs @@ -361,7 +361,10 @@ internal static class ValidationHelper /// public static void ValidateNoNullArgument(object obj, string argumentName) { - ArgumentNullException.ThrowIfNull(obj, argumentName); + if (obj == null) + { + throw new ArgumentNullException(argumentName); + } } /// diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs b/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs index 4bc9ebef28d..b0839c6ccd2 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs @@ -214,7 +214,10 @@ public static void AddChild(FrameworkElement parent, FrameworkElement element) { ArgumentNullException.ThrowIfNull(element); - ArgumentNullException.ThrowIfNull(parent, nameof(element)); + if (parent == null) + { + throw new ArgumentNullException("element"); + } ContentControl parentContentControl = parent as ContentControl; @@ -367,7 +370,10 @@ public static T FindVisualAncestorData(this DependencyObject obj) /// The specified value is a null reference. public static T FindVisualAncestor(this DependencyObject @object) where T : class { - ArgumentNullException.ThrowIfNull(@object, nameof(@object)); + if (@object == null) + { + throw new ArgumentNullException("object"); + } DependencyObject parent = VisualTreeHelper.GetParent(@object); diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs b/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs index 2d590904097..cf85d79ae36 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs @@ -62,9 +62,7 @@ public string DefaultValue /// public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { - ArgumentNullException.ThrowIfNull(values); - - if (values.Length != 1) + if (values == null || values.Length != 1) { throw new ArgumentNullException("values"); } diff --git a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs index 04fc95e4223..8bc9e024022 100644 --- a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs +++ b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs @@ -79,9 +79,7 @@ public class AllModulesViewModel : INotifyPropertyChanged /// Commands to show. public AllModulesViewModel(Dictionary importedModules, IEnumerable commands) { - ArgumentNullException.ThrowIfNull(commands); - - if (!commands.GetEnumerator().MoveNext()) + if (commands == null || !commands.GetEnumerator().MoveNext()) { throw new ArgumentNullException("commands"); } diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs index 8da4d7f4c78..58a1fbb4c2d 100644 --- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs +++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs @@ -82,8 +82,7 @@ protected TSession[] Session set { - ArgumentNullException.ThrowIfNull(value); - _session = value; + _session = value ?? throw new ArgumentNullException(nameof(value)); _sessionWasSpecified = true; } } diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index e11be68156a..e6b33142457 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -575,12 +575,10 @@ private void ProcessMTUSize(string targetNameOrAddress) } else { - ArgumentNullException.ThrowIfNull(replyResult); - WriteObject(new PingMtuStatus( Source, resolvedTargetName, - replyResult, + replyResult ?? throw new ArgumentNullException(nameof(replyResult)), CurrentMTUSize)); } } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index a27e4e70008..9864deb7772 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -163,7 +163,7 @@ internal override void ProcessResponse(HttpResponseMessage response) private static RestReturnType CheckReturnType(HttpResponseMessage response) { - ArgumentNullException.ThrowIfNull(response); + if (response == null) { throw new ArgumentNullException(nameof(response)); } RestReturnType rt = RestReturnType.Detect; string contentType = ContentHelper.GetContentType(response); diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index 30106d9a1e7..9b92ecbc204 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -835,7 +835,10 @@ internal void Parse(string[] args) for (int i = 0; i < args.Length; i++) { - ArgumentNullException.ThrowIfNull(args[i], CommandLineParameterParserStrings.NullElementInArgs); + if (args[i] is null) + { + throw new ArgumentNullException(nameof(args), CommandLineParameterParserStrings.NullElementInArgs); + } } // Indicates that we've called this method on this instance, and that when it's done, the state variables diff --git a/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProviderTraceListener.cs b/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProviderTraceListener.cs index 1c82891b654..94eb340b21c 100644 --- a/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProviderTraceListener.cs +++ b/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProviderTraceListener.cs @@ -41,7 +41,8 @@ public string Delimiter [SuppressMessage("Microsoft.Usage", "CA2208:InstantiateArgumentExceptionsCorrectly")] set { - ArgumentNullException.ThrowIfNull(value, nameof(Delimiter)); + if (value == null) + throw new ArgumentNullException(nameof(Delimiter)); if (value.Length == 0) throw new ArgumentException(DotNetEventingStrings.Argument_NeedNonemptyDelimiter); diff --git a/src/Microsoft.WSMan.Management/ConfigProvider.cs b/src/Microsoft.WSMan.Management/ConfigProvider.cs index d40e4161188..9429c6ef4e7 100644 --- a/src/Microsoft.WSMan.Management/ConfigProvider.cs +++ b/src/Microsoft.WSMan.Management/ConfigProvider.cs @@ -3413,7 +3413,7 @@ private static string NormalizePath(string path, string host) /// private PSObject GetItemValue(string path) { - if (string.IsNullOrEmpty(path)) + if (string.IsNullOrEmpty(path) || (path.Length == 0)) { throw new ArgumentNullException(path); } diff --git a/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs b/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs index 6a9462f37d4..47b02d7d131 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs @@ -580,9 +580,7 @@ public static class PowerShellAssemblyLoadContextInitializer public static void SetPowerShellAssemblyLoadContext([MarshalAs(UnmanagedType.LPWStr)] string basePaths) { if (string.IsNullOrEmpty(basePaths)) - { throw new ArgumentNullException(nameof(basePaths)); - } PowerShellAssemblyLoadContext.InitializeSingleton(basePaths); } diff --git a/src/System.Management.Automation/engine/ComInterop/Helpers.cs b/src/System.Management.Automation/engine/ComInterop/Helpers.cs index 814e93825a7..513c3126476 100644 --- a/src/System.Management.Automation/engine/ComInterop/Helpers.cs +++ b/src/System.Management.Automation/engine/ComInterop/Helpers.cs @@ -35,7 +35,10 @@ internal static class Requires [System.Diagnostics.Conditional("DEBUG")] internal static void NotNull(object value, string paramName) { - ArgumentNullException.ThrowIfNull(value, paramName); + if (value == null) + { + throw new ArgumentNullException(paramName); + } } [System.Diagnostics.Conditional("DEBUG")] diff --git a/src/System.Management.Automation/engine/DefaultCommandRuntime.cs b/src/System.Management.Automation/engine/DefaultCommandRuntime.cs index 88a2a2ee427..48d74667dae 100644 --- a/src/System.Management.Automation/engine/DefaultCommandRuntime.cs +++ b/src/System.Management.Automation/engine/DefaultCommandRuntime.cs @@ -21,7 +21,8 @@ internal class DefaultCommandRuntime : ICommandRuntime2 /// public DefaultCommandRuntime(List outputList) { - ArgumentNullException.ThrowIfNull(outputList); + if (outputList == null) + throw new System.ArgumentNullException(nameof(outputList)); _output = outputList; } diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 9784cdc9ea8..4c453edfbaf 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -278,9 +278,7 @@ public string[] ParameterName set { - ArgumentNullException.ThrowIfNull(value); - - _parameterNames = value; + _parameterNames = value ?? throw new ArgumentNullException(nameof(value)); _parameterNameWildcards = SessionStateUtilities.CreateWildcardsFromStrings( _parameterNames, WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase); diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 31b8f549b4f..1bbd7f3ad85 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -3805,7 +3805,11 @@ internal PSSnapInInfo ImportCorePSSnapIn() internal PSSnapInInfo ImportPSSnapIn(PSSnapInInfo psSnapInInfo, out PSSnapInException warning) { - ArgumentNullException.ThrowIfNull(psSnapInInfo); + if (psSnapInInfo == null) + { + ArgumentNullException e = new ArgumentNullException(nameof(psSnapInInfo)); + throw e; + } // See if the snapin is already loaded. If has been then there will be an entry in the // Assemblies list for it already... @@ -3981,7 +3985,11 @@ internal static Assembly LoadAssemblyFromFile(string fileName) internal void ImportCmdletsFromAssembly(Assembly assembly, PSModuleInfo module) { - ArgumentNullException.ThrowIfNull(assembly); + if (assembly == null) + { + ArgumentNullException e = new ArgumentNullException(nameof(assembly)); + throw e; + } string assemblyPath = assembly.Location; PSSnapInHelpers.AnalyzePSSnapInAssembly( diff --git a/src/System.Management.Automation/engine/PSClassInfo.cs b/src/System.Management.Automation/engine/PSClassInfo.cs index c73e9250071..fd0ca8d8936 100644 --- a/src/System.Management.Automation/engine/PSClassInfo.cs +++ b/src/System.Management.Automation/engine/PSClassInfo.cs @@ -62,9 +62,7 @@ public sealed class PSClassMemberInfo internal PSClassMemberInfo(string name, string memberType, string defaultValue) { if (string.IsNullOrEmpty(name)) - { throw new ArgumentNullException(nameof(name)); - } this.Name = name; this.TypeName = memberType; diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index bdf85b54e5e..f0b6b499ced 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -1735,7 +1735,10 @@ internal static class Requires { internal static void NotNull(object value, string paramName) { - ArgumentNullException.ThrowIfNull(value, paramName); + if (value == null) + { + throw new ArgumentNullException(paramName); + } } internal static void NotNullOrEmpty(string value, string paramName) @@ -1748,9 +1751,7 @@ internal static void NotNullOrEmpty(string value, string paramName) internal static void NotNullOrEmpty(ICollection value, string paramName) { - ArgumentNullException.ThrowIfNull(value, paramName); - - if (value.Count == 0) + if (value == null || value.Count == 0) { throw new ArgumentNullException(paramName); } diff --git a/src/System.Management.Automation/engine/lang/scriptblock.cs b/src/System.Management.Automation/engine/lang/scriptblock.cs index c00fb9d6e37..be75fecd326 100644 --- a/src/System.Management.Automation/engine/lang/scriptblock.cs +++ b/src/System.Management.Automation/engine/lang/scriptblock.cs @@ -1138,9 +1138,10 @@ public void Begin(bool expectInput, EngineIntrinsics contextToRedirectTo) /// The command you're calling this from (i.e. instance of PSCmdlet or value of $PSCmdlet variable). public void Begin(InternalCommand command) { - ArgumentNullException.ThrowIfNull(command); - - ArgumentNullException.ThrowIfNull(command.MyInvocation, nameof(command)); + if (command == null || command.MyInvocation == null) + { + throw new ArgumentNullException(nameof(command)); + } Begin(command.MyInvocation.ExpectingInput, command.commandRuntime); } diff --git a/src/System.Management.Automation/engine/parser/SafeValues.cs b/src/System.Management.Automation/engine/parser/SafeValues.cs index f551270c372..0e41f87a318 100644 --- a/src/System.Management.Automation/engine/parser/SafeValues.cs +++ b/src/System.Management.Automation/engine/parser/SafeValues.cs @@ -530,10 +530,10 @@ public object VisitIndexExpression(IndexExpressionAst indexExpressionAst) // Get the value of the index and value and call the compiler var index = indexExpressionAst.Index.Accept(this); var target = indexExpressionAst.Target.Accept(this); - - ArgumentNullException.ThrowIfNull(index, nameof(indexExpressionAst)); - - ArgumentNullException.ThrowIfNull(target, nameof(indexExpressionAst)); + if (index == null || target == null) + { + throw new ArgumentNullException(nameof(indexExpressionAst)); + } return GetIndexedValueFromTarget(target, index); } diff --git a/src/System.Management.Automation/engine/remoting/commands/JobRepository.cs b/src/System.Management.Automation/engine/remoting/commands/JobRepository.cs index 29d107a074f..7bb52abcb1e 100644 --- a/src/System.Management.Automation/engine/remoting/commands/JobRepository.cs +++ b/src/System.Management.Automation/engine/remoting/commands/JobRepository.cs @@ -19,7 +19,10 @@ public abstract class Repository where T : class /// Object to add. public void Add(T item) { - ArgumentNullException.ThrowIfNull(item, _identifier); + if (item == null) + { + throw new ArgumentNullException(_identifier); + } lock (_syncObject) { @@ -42,7 +45,10 @@ public void Add(T item) /// Object to remove. public void Remove(T item) { - ArgumentNullException.ThrowIfNull(item, _identifier); + if (item == null) + { + throw new ArgumentNullException(_identifier); + } lock (_syncObject) { diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 5c2ff6d89c4..30db2e27180 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -7969,6 +7969,7 @@ internal static bool CreateJunction(string path, string target) { throw new ArgumentNullException(nameof(target)); } + using (SafeHandle handle = WinOpenReparsePoint(path, FileAccess.Write)) { byte[] mountPointBytes = Encoding.Unicode.GetBytes(NonInterpretedPathPrefix + Path.GetFullPath(target)); diff --git a/src/System.Management.Automation/namespaces/TransactedRegistryKey.cs b/src/System.Management.Automation/namespaces/TransactedRegistryKey.cs index 19e05ce99a7..a7bd5dc177b 100644 --- a/src/System.Management.Automation/namespaces/TransactedRegistryKey.cs +++ b/src/System.Management.Automation/namespaces/TransactedRegistryKey.cs @@ -1388,7 +1388,8 @@ public void SetValue(string name, object value) [ComVisible(false)] public unsafe void SetValue(string name, object value, RegistryValueKind valueKind) { - ArgumentNullException.ThrowIfNull(value, RegistryProviderStrings.Arg_Value); + if (value == null) + throw new ArgumentNullException(RegistryProviderStrings.Arg_Value); if (name != null && name.Length > MaxValueNameLength) { @@ -2030,7 +2031,10 @@ private RegistryKeyPermissionCheck GetSubKeyPermissionCheck(bool subkeyWritable) private static void ValidateKeyName(string name) { - ArgumentNullException.ThrowIfNull(name, RegistryProviderStrings.Arg_Name); + if (name == null) + { + throw new ArgumentNullException(RegistryProviderStrings.Arg_Name); + } int nextSlash = name.IndexOf('\\'); int current = 0; diff --git a/src/System.Management.Automation/utils/BackgroundDispatcher.cs b/src/System.Management.Automation/utils/BackgroundDispatcher.cs index ac74a32d352..8ef105ce0ea 100644 --- a/src/System.Management.Automation/utils/BackgroundDispatcher.cs +++ b/src/System.Management.Automation/utils/BackgroundDispatcher.cs @@ -70,8 +70,7 @@ public BackgroundDispatcher(EventProvider transferProvider, EventDescriptor tran // internal for unit testing only. Otherwise, would be private. internal BackgroundDispatcher(IMethodInvoker etwActivityMethodInvoker) { - ArgumentNullException.ThrowIfNull(etwActivityMethodInvoker); - _etwActivityMethodInvoker = etwActivityMethodInvoker; + _etwActivityMethodInvoker = etwActivityMethodInvoker ?? throw new ArgumentNullException(nameof(etwActivityMethodInvoker)); _invokerWaitCallback = DoInvoker; } diff --git a/src/System.Management.Automation/utils/ParserException.cs b/src/System.Management.Automation/utils/ParserException.cs index 9087c374b69..168c32b796c 100644 --- a/src/System.Management.Automation/utils/ParserException.cs +++ b/src/System.Management.Automation/utils/ParserException.cs @@ -129,9 +129,7 @@ public ParseException(string message, /// The collection of error messages. public ParseException(ParseError[] errors) { - ArgumentNullException.ThrowIfNull(errors); - - if (errors.Length == 0) + if ((errors == null) || (errors.Length == 0)) { throw new ArgumentNullException(nameof(errors)); } diff --git a/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs b/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs index 33e03e67a47..917e04a5e69 100644 --- a/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs +++ b/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs @@ -107,10 +107,8 @@ protected CounterSetRegistrarBase( CounterSetId = counterSetId; CounterSetInstType = counterSetInstType; CounterSetName = counterSetName; - - ArgumentNullException.ThrowIfNull(counterInfoArray); - - if (counterInfoArray.Length == 0) + if ((counterInfoArray == null) + || (counterInfoArray.Length == 0)) { throw new ArgumentNullException(nameof(counterInfoArray)); }