mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Replace ArgumentNullException(nameof()) -> ArgumentNullException.ThrowIfNull() 2 (#18792)
This commit is contained in:
@@ -149,10 +149,8 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
|
||||
uint operationTimeout)
|
||||
{
|
||||
DebugHelper.WriteLogEx("queryDialect = '{0}'; queryExpression = '{1}'", 0, queryDialect, queryExpression);
|
||||
if (cimSession == null)
|
||||
{
|
||||
throw new ArgumentNullException(string.Format(CultureInfo.CurrentUICulture, CimCmdletStrings.NullArgument, nameof(cimSession)));
|
||||
}
|
||||
|
||||
ArgumentNullException.ThrowIfNull(cimSession, string.Format(CultureInfo.CurrentUICulture, CimCmdletStrings.NullArgument, nameof(cimSession)));
|
||||
|
||||
this.TargetComputerName = cimSession.ComputerName;
|
||||
CimSessionProxy proxy = CreateSessionProxy(cimSession, operationTimeout);
|
||||
|
||||
@@ -371,10 +371,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
|
||||
/// <param name="argumentName"></param>
|
||||
public static void ValidateNoNullArgument(object obj, string argumentName)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
throw new ArgumentNullException(argumentName);
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(obj, argumentName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -214,10 +214,7 @@ namespace Microsoft.Management.UI.Internal
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(element);
|
||||
|
||||
if (parent == null)
|
||||
{
|
||||
throw new ArgumentNullException("element");
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(parent, nameof(element));
|
||||
|
||||
ContentControl parentContentControl = parent as ContentControl;
|
||||
|
||||
@@ -370,10 +367,7 @@ namespace Microsoft.Management.UI.Internal
|
||||
/// <exception cref="ArgumentNullException">The specified value is a null reference.</exception>
|
||||
public static T FindVisualAncestor<T>(this DependencyObject @object) where T : class
|
||||
{
|
||||
if (@object == null)
|
||||
{
|
||||
throw new ArgumentNullException("object");
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(@object, nameof(@object));
|
||||
|
||||
DependencyObject parent = VisualTreeHelper.GetParent(@object);
|
||||
|
||||
|
||||
+3
-1
@@ -62,7 +62,9 @@ namespace Microsoft.Management.UI.Internal
|
||||
/// </remarks>
|
||||
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
|
||||
{
|
||||
if (values == null || values.Length != 1)
|
||||
ArgumentNullException.ThrowIfNull(values);
|
||||
|
||||
if (values.Length != 1)
|
||||
{
|
||||
throw new ArgumentNullException("values");
|
||||
}
|
||||
|
||||
@@ -79,7 +79,9 @@ namespace Microsoft.PowerShell.Commands.ShowCommandInternal
|
||||
/// <param name="commands">Commands to show.</param>
|
||||
public AllModulesViewModel(Dictionary<string, ShowCommandModuleInfo> importedModules, IEnumerable<ShowCommandCommandInfo> commands)
|
||||
{
|
||||
if (commands == null || !commands.GetEnumerator().MoveNext())
|
||||
ArgumentNullException.ThrowIfNull(commands);
|
||||
|
||||
if (!commands.GetEnumerator().MoveNext())
|
||||
{
|
||||
throw new ArgumentNullException("commands");
|
||||
}
|
||||
|
||||
+2
-1
@@ -82,7 +82,8 @@ namespace Microsoft.PowerShell.Cmdletization
|
||||
|
||||
set
|
||||
{
|
||||
_session = value ?? throw new ArgumentNullException(nameof(value));
|
||||
ArgumentNullException.ThrowIfNull(value);
|
||||
_session = value;
|
||||
_sessionWasSpecified = true;
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -575,10 +575,12 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
else
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(replyResult);
|
||||
|
||||
WriteObject(new PingMtuStatus(
|
||||
Source,
|
||||
resolvedTargetName,
|
||||
replyResult ?? throw new ArgumentNullException(nameof(replyResult)),
|
||||
replyResult,
|
||||
CurrentMTUSize));
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -189,7 +189,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
private static RestReturnType CheckReturnType(HttpResponseMessage response)
|
||||
{
|
||||
if (response == null) { throw new ArgumentNullException(nameof(response)); }
|
||||
ArgumentNullException.ThrowIfNull(response);
|
||||
|
||||
RestReturnType rt = RestReturnType.Detect;
|
||||
string contentType = ContentHelper.GetContentType(response);
|
||||
|
||||
@@ -833,10 +833,7 @@ namespace Microsoft.PowerShell
|
||||
|
||||
for (int i = 0; i < args.Length; i++)
|
||||
{
|
||||
if (args[i] is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(args), CommandLineParameterParserStrings.NullElementInArgs);
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(args[i], CommandLineParameterParserStrings.NullElementInArgs);
|
||||
}
|
||||
|
||||
// Indicates that we've called this method on this instance, and that when it's done, the state variables
|
||||
|
||||
+1
-2
@@ -41,8 +41,7 @@ namespace System.Diagnostics.Eventing
|
||||
[SuppressMessage("Microsoft.Usage", "CA2208:InstantiateArgumentExceptionsCorrectly")]
|
||||
set
|
||||
{
|
||||
if (value == null)
|
||||
throw new ArgumentNullException(nameof(Delimiter));
|
||||
ArgumentNullException.ThrowIfNull(value, nameof(Delimiter));
|
||||
|
||||
if (value.Length == 0)
|
||||
throw new ArgumentException(DotNetEventingStrings.Argument_NeedNonemptyDelimiter);
|
||||
|
||||
@@ -3413,7 +3413,7 @@ namespace Microsoft.WSMan.Management
|
||||
/// </exception>
|
||||
private PSObject GetItemValue(string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path) || (path.Length == 0))
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
throw new ArgumentNullException(path);
|
||||
}
|
||||
|
||||
@@ -580,7 +580,9 @@ namespace System.Management.Automation
|
||||
public static void SetPowerShellAssemblyLoadContext([MarshalAs(UnmanagedType.LPWStr)] string basePaths)
|
||||
{
|
||||
if (string.IsNullOrEmpty(basePaths))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(basePaths));
|
||||
}
|
||||
|
||||
PowerShellAssemblyLoadContext.InitializeSingleton(basePaths);
|
||||
}
|
||||
|
||||
@@ -35,10 +35,7 @@ namespace System.Management.Automation.ComInterop
|
||||
[System.Diagnostics.Conditional("DEBUG")]
|
||||
internal static void NotNull(object value, string paramName)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw new ArgumentNullException(paramName);
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(value, paramName);
|
||||
}
|
||||
|
||||
[System.Diagnostics.Conditional("DEBUG")]
|
||||
|
||||
@@ -21,8 +21,7 @@ namespace System.Management.Automation
|
||||
/// </summary>
|
||||
public DefaultCommandRuntime(List<object> outputList)
|
||||
{
|
||||
if (outputList == null)
|
||||
throw new System.ArgumentNullException(nameof(outputList));
|
||||
ArgumentNullException.ThrowIfNull(outputList);
|
||||
|
||||
_output = outputList;
|
||||
}
|
||||
|
||||
@@ -278,7 +278,9 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
set
|
||||
{
|
||||
_parameterNames = value ?? throw new ArgumentNullException(nameof(value));
|
||||
ArgumentNullException.ThrowIfNull(value);
|
||||
|
||||
_parameterNames = value;
|
||||
_parameterNameWildcards = SessionStateUtilities.CreateWildcardsFromStrings(
|
||||
_parameterNames,
|
||||
WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase);
|
||||
|
||||
@@ -3738,11 +3738,7 @@ namespace System.Management.Automation.Runspaces
|
||||
|
||||
internal PSSnapInInfo ImportPSSnapIn(PSSnapInInfo psSnapInInfo, out PSSnapInException warning)
|
||||
{
|
||||
if (psSnapInInfo == null)
|
||||
{
|
||||
ArgumentNullException e = new ArgumentNullException(nameof(psSnapInInfo));
|
||||
throw e;
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(psSnapInInfo);
|
||||
|
||||
// See if the snapin is already loaded. If has been then there will be an entry in the
|
||||
// Assemblies list for it already...
|
||||
@@ -3918,11 +3914,7 @@ namespace System.Management.Automation.Runspaces
|
||||
|
||||
internal void ImportCmdletsFromAssembly(Assembly assembly, PSModuleInfo module)
|
||||
{
|
||||
if (assembly == null)
|
||||
{
|
||||
ArgumentNullException e = new ArgumentNullException(nameof(assembly));
|
||||
throw e;
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(assembly);
|
||||
|
||||
string assemblyPath = assembly.Location;
|
||||
PSSnapInHelpers.AnalyzePSSnapInAssembly(
|
||||
|
||||
@@ -62,7 +62,9 @@ namespace System.Management.Automation
|
||||
internal PSClassMemberInfo(string name, string memberType, string defaultValue)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(name));
|
||||
}
|
||||
|
||||
this.Name = name;
|
||||
this.TypeName = memberType;
|
||||
|
||||
@@ -1740,10 +1740,7 @@ namespace System.Management.Automation.Internal
|
||||
{
|
||||
internal static void NotNull(object value, string paramName)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw new ArgumentNullException(paramName);
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(value, paramName);
|
||||
}
|
||||
|
||||
internal static void NotNullOrEmpty(string value, string paramName)
|
||||
@@ -1756,7 +1753,9 @@ namespace System.Management.Automation.Internal
|
||||
|
||||
internal static void NotNullOrEmpty(ICollection value, string paramName)
|
||||
{
|
||||
if (value == null || value.Count == 0)
|
||||
ArgumentNullException.ThrowIfNull(value, paramName);
|
||||
|
||||
if (value.Count == 0)
|
||||
{
|
||||
throw new ArgumentNullException(paramName);
|
||||
}
|
||||
|
||||
@@ -1138,10 +1138,9 @@ namespace System.Management.Automation
|
||||
/// <param name="command">The command you're calling this from (i.e. instance of PSCmdlet or value of $PSCmdlet variable).</param>
|
||||
public void Begin(InternalCommand command)
|
||||
{
|
||||
if (command == null || command.MyInvocation == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(command));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(command);
|
||||
|
||||
ArgumentNullException.ThrowIfNull(command.MyInvocation, nameof(command));
|
||||
|
||||
Begin(command.MyInvocation.ExpectingInput, command.commandRuntime);
|
||||
}
|
||||
|
||||
@@ -530,10 +530,10 @@ namespace System.Management.Automation.Language
|
||||
// Get the value of the index and value and call the compiler
|
||||
var index = indexExpressionAst.Index.Accept(this);
|
||||
var target = indexExpressionAst.Target.Accept(this);
|
||||
if (index == null || target == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(indexExpressionAst));
|
||||
}
|
||||
|
||||
ArgumentNullException.ThrowIfNull(index, nameof(indexExpressionAst));
|
||||
|
||||
ArgumentNullException.ThrowIfNull(target, nameof(indexExpressionAst));
|
||||
|
||||
return GetIndexedValueFromTarget(target, index);
|
||||
}
|
||||
|
||||
@@ -19,10 +19,7 @@ namespace System.Management.Automation
|
||||
/// <param name="item">Object to add.</param>
|
||||
public void Add(T item)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
throw new ArgumentNullException(_identifier);
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(item, _identifier);
|
||||
|
||||
lock (_syncObject)
|
||||
{
|
||||
@@ -45,10 +42,7 @@ namespace System.Management.Automation
|
||||
/// <param name="item">Object to remove.</param>
|
||||
public void Remove(T item)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
throw new ArgumentNullException(_identifier);
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(item, _identifier);
|
||||
|
||||
lock (_syncObject)
|
||||
{
|
||||
|
||||
@@ -7884,7 +7884,6 @@ namespace Microsoft.PowerShell.Commands
|
||||
{
|
||||
throw new ArgumentNullException(nameof(target));
|
||||
}
|
||||
|
||||
using (SafeHandle handle = WinOpenReparsePoint(path, FileAccess.Write))
|
||||
{
|
||||
byte[] mountPointBytes = Encoding.Unicode.GetBytes(NonInterpretedPathPrefix + Path.GetFullPath(target));
|
||||
|
||||
@@ -1388,8 +1388,7 @@ namespace Microsoft.PowerShell.Commands.Internal
|
||||
[ComVisible(false)]
|
||||
public unsafe void SetValue(string name, object value, RegistryValueKind valueKind)
|
||||
{
|
||||
if (value == null)
|
||||
throw new ArgumentNullException(RegistryProviderStrings.Arg_Value);
|
||||
ArgumentNullException.ThrowIfNull(value, RegistryProviderStrings.Arg_Value);
|
||||
|
||||
if (name != null && name.Length > MaxValueNameLength)
|
||||
{
|
||||
@@ -2031,10 +2030,7 @@ namespace Microsoft.PowerShell.Commands.Internal
|
||||
|
||||
private static void ValidateKeyName(string name)
|
||||
{
|
||||
if (name == null)
|
||||
{
|
||||
throw new ArgumentNullException(RegistryProviderStrings.Arg_Name);
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(name, RegistryProviderStrings.Arg_Name);
|
||||
|
||||
int nextSlash = name.IndexOf('\\');
|
||||
int current = 0;
|
||||
|
||||
@@ -70,7 +70,8 @@ namespace System.Management.Automation
|
||||
// internal for unit testing only. Otherwise, would be private.
|
||||
internal BackgroundDispatcher(IMethodInvoker etwActivityMethodInvoker)
|
||||
{
|
||||
_etwActivityMethodInvoker = etwActivityMethodInvoker ?? throw new ArgumentNullException(nameof(etwActivityMethodInvoker));
|
||||
ArgumentNullException.ThrowIfNull(etwActivityMethodInvoker);
|
||||
_etwActivityMethodInvoker = etwActivityMethodInvoker;
|
||||
_invokerWaitCallback = DoInvoker;
|
||||
}
|
||||
|
||||
|
||||
@@ -131,7 +131,9 @@ namespace System.Management.Automation
|
||||
Justification = "ErrorRecord is not overridden in classes deriving from ParseException")]
|
||||
public ParseException(Language.ParseError[] errors)
|
||||
{
|
||||
if ((errors == null) || (errors.Length == 0))
|
||||
ArgumentNullException.ThrowIfNull(errors);
|
||||
|
||||
if (errors.Length == 0)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(errors));
|
||||
}
|
||||
|
||||
@@ -107,8 +107,10 @@ namespace System.Management.Automation.PerformanceData
|
||||
CounterSetId = counterSetId;
|
||||
CounterSetInstType = counterSetInstType;
|
||||
CounterSetName = counterSetName;
|
||||
if ((counterInfoArray == null)
|
||||
|| (counterInfoArray.Length == 0))
|
||||
|
||||
ArgumentNullException.ThrowIfNull(counterInfoArray);
|
||||
|
||||
if (counterInfoArray.Length == 0)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(counterInfoArray));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user