Replace ArrayList with List<T> (#10333)

This commit is contained in:
Ilya
2019-09-05 10:54:18 -07:00
committed by Aditya Patwardhan
parent f6c220cdd9
commit fbf4f6c11b
14 changed files with 59 additions and 62 deletions
@@ -338,13 +338,9 @@ namespace Microsoft.PowerShell.Commands
if (ReadCount <= 0 || (ReadCount >= tailResultQueue.Count && ReadCount != 1))
{
count = tailResultQueue.Count;
ArrayList outputList = new ArrayList();
while (tailResultQueue.Count > 0)
{
outputList.Add(tailResultQueue.Dequeue());
}
// Write out the content as an array of objects
WriteContentObject(outputList.ToArray(), count, holder.PathInfo, currentContext);
WriteContentObject(tailResultQueue.ToArray(), count, holder.PathInfo, currentContext);
}
else if (ReadCount == 1)
{
@@ -356,7 +352,7 @@ namespace Microsoft.PowerShell.Commands
{
while (tailResultQueue.Count >= ReadCount)
{
ArrayList outputList = new ArrayList();
var outputList = new List<object>((int)ReadCount);
for (int idx = 0; idx < ReadCount; idx++, count++)
outputList.Add(tailResultQueue.Dequeue());
// Write out the content as an array of objects
@@ -366,11 +362,8 @@ namespace Microsoft.PowerShell.Commands
int remainder = tailResultQueue.Count;
if (remainder > 0)
{
ArrayList outputList = new ArrayList();
for (; remainder > 0; remainder--, count++)
outputList.Add(tailResultQueue.Dequeue());
// Write out the content as an array of objects
WriteContentObject(outputList.ToArray(), count, holder.PathInfo, currentContext);
WriteContentObject(tailResultQueue.ToArray(), count, holder.PathInfo, currentContext);
}
}
}
@@ -333,7 +333,7 @@ namespace Microsoft.PowerShell.Commands
{
Collection<PathInfo> pathInfos = ResolvePaths(unfilteredPaths, true, false, currentContext);
ArrayList paths = new ArrayList();
var paths = new List<string>();
foreach (PathInfo pathInfo in pathInfos)
{
@@ -343,7 +343,7 @@ namespace Microsoft.PowerShell.Commands
}
}
return (string[])paths.ToArray(typeof(string));
return paths.ToArray();
}
#endregion protected members
@@ -359,7 +359,7 @@ namespace Microsoft.PowerShell.Commands
private void InitializeResolvedNameMshParameters()
{
// temp list of properties with wildcards resolved
ArrayList resolvedNameProperty = new ArrayList();
var resolvedNameProperty = new List<object>();
foreach (MshParameter p in _propertyMshParameterList)
{
@@ -747,7 +747,7 @@ namespace Microsoft.PowerShell.Commands
{
if (_valueList == null)
{
_valueList = new ArrayList();
_valueList = new List<object>();
}
_valueList.Add(Value);
@@ -759,7 +759,7 @@ namespace Microsoft.PowerShell.Commands
}
}
private ArrayList _valueList;
private List<object> _valueList;
/// <summary>
/// Sets the variable if the name was specified as a formal parameter
@@ -2670,7 +2670,7 @@ namespace Microsoft.PowerShell
// Output any training suggestions
try
{
ArrayList suggestions = HostUtilities.GetSuggestion(_parent.Runspace);
List<string> suggestions = HostUtilities.GetSuggestion(_parent.Runspace);
if (suggestions.Count > 0)
{
@@ -80,7 +80,7 @@ namespace Microsoft.PowerShell.Commands
//
// list of files that were not found
//
private ArrayList _filesNotFound = new ArrayList();
private List<string> _filesNotFound = new List<string>();
/// <summary>
/// Initializes a new instance of the GetPfxCertificateCommand
@@ -2,7 +2,7 @@
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Internal;
@@ -88,7 +88,7 @@ namespace Microsoft.PowerShell.Commands
if (Context.CurrentCommandProcessor.CommandRuntime.OutVarList != null)
{
_outVarResults = new ArrayList();
_outVarResults = new List<PSObject>();
}
}
@@ -162,7 +162,7 @@ namespace Microsoft.PowerShell.Commands
}
}
private ArrayList _outVarResults = null;
private List<PSObject> _outVarResults = null;
private IDisposable _transcribeOnlyCookie = null;
}
@@ -72,11 +72,11 @@ namespace System.Management.Automation
$formatString -f [string]::Join(', ', (Get-Command $lastError.TargetObject -UseFuzzyMatch | Select-Object -First 10 -Unique -ExpandProperty Name))
";
private static ArrayList s_suggestions = InitializeSuggestions();
private static List<Hashtable> s_suggestions = InitializeSuggestions();
private static ArrayList InitializeSuggestions()
private static List<Hashtable> InitializeSuggestions()
{
ArrayList suggestions = new ArrayList(
var suggestions = new List<Hashtable>(
new Hashtable[]
{
NewSuggestion(
@@ -307,10 +307,10 @@ namespace System.Management.Automation
return returnValue.ToString();
}
internal static ArrayList GetSuggestion(Runspace runspace)
internal static List<string> GetSuggestion(Runspace runspace)
{
LocalRunspace localRunspace = runspace as LocalRunspace;
if (localRunspace == null) { return new ArrayList(); }
if (localRunspace == null) { return new List<string>(); }
// Get the last value of $?
bool questionMarkVariableValue = localRunspace.ExecutionContext.QuestionMarkVariableValue;
@@ -320,7 +320,7 @@ namespace System.Management.Automation
HistoryInfo[] entries = history.GetEntries(-1, 1, true);
if (entries.Length == 0)
return new ArrayList();
return new List<string>();
HistoryInfo lastHistory = entries[0];
@@ -363,7 +363,7 @@ namespace System.Management.Automation
Runspace.DefaultRunspace = runspace;
}
ArrayList suggestions = null;
List<string> suggestions = null;
try
{
@@ -383,9 +383,9 @@ namespace System.Management.Automation
}
[SuppressMessage("Microsoft.Usage", "CA2208:InstantiateArgumentExceptionsCorrectly")]
internal static ArrayList GetSuggestion(HistoryInfo lastHistory, object lastError, ArrayList errorList)
internal static List<string> GetSuggestion(HistoryInfo lastHistory, object lastError, ArrayList errorList)
{
ArrayList returnSuggestions = new ArrayList();
var returnSuggestions = new List<string>();
PSModuleInfo invocationModule = new PSModuleInfo(true);
invocationModule.SessionState.PSVariable.Set("lastHistory", lastHistory);
@@ -32,7 +32,7 @@ namespace System.Management.Automation
{
#region Private Members
private ArrayList _runningPipelines = new ArrayList();
private List<RemotePipeline> _runningPipelines = new List<RemotePipeline>();
private object _syncRoot = new object();
private RunspaceStateInfo _runspaceStateInfo = new RunspaceStateInfo(RunspaceState.BeforeOpen);
private bool _bSessionStateProxyCallInProgress = false;
@@ -1522,7 +1522,7 @@ namespace System.Management.Automation
lock (_syncRoot)
{
runningPipelines = (RemotePipeline[])_runningPipelines.ToArray(typeof(RemotePipeline));
runningPipelines = _runningPipelines.ToArray();
}
if (runningPipelines.Length > 0)
@@ -1912,7 +1912,7 @@ namespace System.Management.Automation
string computerName = GetPropertyValue<string>(dataAsPSObject, RemoteDataNameStrings.PSEventArgsComputerName);
Guid runspaceId = GetPropertyValue<Guid>(dataAsPSObject, RemoteDataNameStrings.PSEventArgsRunspaceId);
ArrayList sourceArgs = new ArrayList();
var sourceArgs = new List<object>();
foreach (object argument in RemotingDecoder.EnumerateListProperty<object>(dataAsPSObject, RemoteDataNameStrings.PSEventArgsSourceArgs))
{
sourceArgs.Add(argument);
@@ -1858,7 +1858,7 @@ namespace System.Management.Automation.Remoting
{
string customizationString = customization.ToString();
ArrayList customizationValue = new ArrayList();
var customizationValue = new List<object>();
// First, take all values from the master config table
if (_configHash.ContainsKey(customizationString))
@@ -1866,7 +1866,7 @@ namespace System.Management.Automation.Remoting
IEnumerable existingValueAsCollection = LanguagePrimitives.GetEnumerable(_configHash[customization]);
if (existingValueAsCollection != null)
{
foreach (Object value in existingValueAsCollection)
foreach (object value in existingValueAsCollection)
{
customizationValue.Add(value);
}
@@ -1881,7 +1881,7 @@ namespace System.Management.Automation.Remoting
IEnumerable newValueAsCollection = LanguagePrimitives.GetEnumerable(childConfigHash[customization]);
if (newValueAsCollection != null)
{
foreach (Object value in newValueAsCollection)
foreach (object value in newValueAsCollection)
{
customizationValue.Add(value);
}
@@ -1,9 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
namespace System.Management.Automation
{
@@ -134,7 +133,7 @@ namespace System.Management.Automation
/// <summary>
/// This tracks all live TraceFrame objects, which forms a stack.
/// </summary>
private ArrayList _traceFrames = new ArrayList();
private readonly List<TraceFrame> _traceFrames = new List<TraceFrame>();
/// <summary>
/// This is the API to use for starting a help trace scope.
@@ -160,7 +159,7 @@ namespace System.Management.Automation
if (_traceFrames.Count <= 0)
return;
TraceFrame traceFrame = (TraceFrame)_traceFrames[_traceFrames.Count - 1];
TraceFrame traceFrame = _traceFrames[_traceFrames.Count - 1];
traceFrame.TraceError(errorRecord);
}
@@ -175,7 +174,7 @@ namespace System.Management.Automation
if (_traceFrames.Count <= 0)
return;
TraceFrame traceFrame = (TraceFrame)_traceFrames[_traceFrames.Count - 1];
TraceFrame traceFrame = _traceFrames[_traceFrames.Count - 1];
traceFrame.TraceErrors(errorRecords);
}
@@ -185,7 +184,7 @@ namespace System.Management.Automation
if (_traceFrames.Count <= 0)
return;
TraceFrame lastFrame = (TraceFrame)_traceFrames[_traceFrames.Count - 1];
TraceFrame lastFrame = _traceFrames[_traceFrames.Count - 1];
if (lastFrame == traceFrame)
{
@@ -118,7 +118,7 @@ namespace System.Management.Automation
#if UNIX
// On Linux, file names are case sensitive, so we need to add
// extra logic to select the files that match the given pattern.
ArrayList result = new ArrayList();
var result = new List<string>();
string[] files = Directory.GetFiles(path);
var wildcardPattern = WildcardPattern.ContainsWildcardCharacters(pattern)
@@ -143,7 +143,7 @@ namespace System.Management.Automation
}
}
return (string[])result.ToArray(typeof(string));
return result.ToArray();
#else
return Directory.GetFiles(path, pattern);
#endif
@@ -344,7 +344,7 @@ namespace Microsoft.PowerShell.Commands
s_tracer.WriteLine("blocks requested = {0}", readCount);
ArrayList blocks = new ArrayList();
var blocks = new List<object>();
bool readToEnd = (readCount <= 0);
if (_alreadyDetectEncoding && _reader.BaseStream.Position == 0)
@@ -367,19 +367,19 @@ namespace Microsoft.PowerShell.Commands
if (_usingByteEncoding)
{
if (!ReadByteEncoded(waitChanges, blocks, false))
if (!ReadByteEncoded(waitChanges, blocks, readBackward: false))
break;
}
else
{
if (_usingDelimiter || _isRawStream)
{
if (!ReadDelimited(waitChanges, blocks, false, _delimiter))
if (!ReadDelimited(waitChanges, blocks, readBackward: false, _delimiter))
break;
}
else
{
if (!ReadByLine(waitChanges, blocks, false))
if (!ReadByLine(waitChanges, blocks, readBackward: false))
break;
}
}
@@ -445,7 +445,7 @@ namespace Microsoft.PowerShell.Commands
s_tracer.WriteLine("blocks seek backwards = {0}", backCount);
ArrayList blocks = new ArrayList();
var blocks = new List<object>();
if (_reader != null)
{
// Make the reader automatically detect the encoding
@@ -463,13 +463,18 @@ namespace Microsoft.PowerShell.Commands
return;
}
StringBuilder builder = new StringBuilder();
foreach (char character in _delimiter)
{
builder.Insert(0, character);
}
string actualDelimiter = string.Create(
_delimiter.Length,
_delimiter,
(chars, buf) =>
{
for (int i = 0, j = buf.Length - 1; i < chars.Length; i++, j--)
{
chars[i] = buf[j];
}
}
);
string actualDelimiter = builder.ToString();
long currentBlock = 0;
string lastDelimiterMatch = null;
@@ -488,14 +493,14 @@ namespace Microsoft.PowerShell.Commands
{
if (_usingByteEncoding)
{
if (!ReadByteEncoded(false, blocks, true))
if (!ReadByteEncoded(waitChanges: false, blocks, readBackward: true))
break;
}
else
{
if (_usingDelimiter)
{
if (!ReadDelimited(false, blocks, true, actualDelimiter))
if (!ReadDelimited(waitChanges: false, blocks, readBackward: true, actualDelimiter))
break;
// If the delimiter is at the end of the file, we need to read one more
// to get to the right position. For example:
@@ -508,7 +513,7 @@ namespace Microsoft.PowerShell.Commands
}
else
{
if (!ReadByLine(false, blocks, true))
if (!ReadByLine(waitChanges: false, blocks, readBackward: true))
break;
}
}
@@ -553,7 +558,7 @@ namespace Microsoft.PowerShell.Commands
}
}
private bool ReadByLine(bool waitChanges, ArrayList blocks, bool readBackward)
private bool ReadByLine(bool waitChanges, List<object> blocks, bool readBackward)
{
// Reading lines as strings
string line = readBackward ? _backReader.ReadLine() : _reader.ReadLine();
@@ -581,7 +586,7 @@ namespace Microsoft.PowerShell.Commands
return true;
}
private bool ReadDelimited(bool waitChanges, ArrayList blocks, bool readBackward, string actualDelimiter)
private bool ReadDelimited(bool waitChanges, List<object> blocks, bool readBackward, string actualDelimiter)
{
if (_isRawStream)
{
@@ -705,7 +710,7 @@ namespace Microsoft.PowerShell.Commands
}
}
private bool ReadByteEncoded(bool waitChanges, ArrayList blocks, bool readBack)
private bool ReadByteEncoded(bool waitChanges, List<object> blocks, bool readBackward)
{
if (_isRawStream)
{
@@ -738,7 +743,7 @@ namespace Microsoft.PowerShell.Commands
}
}
if (readBack)
if (readBackward)
{
if (_stream.Position == 0)
{