Skip to content

Commit

Permalink
Convert some uses of ArrayList to List<T> (dotnet#53515)
Browse files Browse the repository at this point in the history
ArrayList.ToArray(Type) has to lookup the array type in the type loader structures. It makes it slow and potentially AOT unfriendly since the code required to support the array type may not exist.
Convert most uses of ArrayList.ToArray in libraries to use List<T> if possible, or ArrayList.CopyTo if the conversion to List<T> was not straightforward.

Simplified a few other places that created arrays while I was on it.
  • Loading branch information
jkotas authored Jun 1, 2021
1 parent ffb095a commit c69254f
Show file tree
Hide file tree
Showing 23 changed files with 145 additions and 130 deletions.
15 changes: 7 additions & 8 deletions src/libraries/Microsoft.XmlSerializer.Generator/src/Sgen.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
Expand All @@ -28,9 +27,9 @@ public static int Main(string[] args)
private int Run(string[] args)
{
string assembly = null;
List<string> types = new List<string>();
var types = new List<string>();
string codePath = null;
var errs = new ArrayList();
var errs = new List<string>();
bool force = false;
bool proxyOnly = false;
bool disableRun = true;
Expand Down Expand Up @@ -247,8 +246,8 @@ private void GenerateFile(List<string> typeNames, string assemblyName, bool prox
}
}

var mappings = new ArrayList();
var importedTypes = new ArrayList();
var mappings = new List<XmlMapping>();
var importedTypes = new List<Type>();
var importer = new XmlReflectionImporter();

for (int i = 0; i < types.Length; i++)
Expand Down Expand Up @@ -296,8 +295,8 @@ private void GenerateFile(List<string> typeNames, string assemblyName, bool prox

if (importedTypes.Count > 0)
{
var serializableTypes = (Type[])importedTypes.ToArray(typeof(Type));
var allMappings = (XmlMapping[])mappings.ToArray(typeof(XmlMapping));
var serializableTypes = importedTypes.ToArray();
var allMappings = mappings.ToArray();

bool gac = assembly.GlobalAssemblyCache;
outputDirectory = outputDirectory == null ? (gac ? Environment.CurrentDirectory : Path.GetDirectoryName(assembly.Location)) : outputDirectory;
Expand Down Expand Up @@ -406,7 +405,7 @@ private bool ShortNameArgumentMatch(string arg, string shortName)
return arg.Equals(shortName, StringComparison.InvariantCultureIgnoreCase);
}

private void ImportType(Type type, ArrayList mappings, ArrayList importedTypes, bool verbose, XmlReflectionImporter importer, bool parsableerrors)
private void ImportType(Type type, List<XmlMapping> mappings, List<Type> importedTypes, bool verbose, XmlReflectionImporter importer, bool parsableerrors)
{
XmlTypeMapping xmlTypeMapping = null;
var localImporter = new XmlReflectionImporter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ public PropertyDescriptorCollection Properties
propList.AddRange(child.Properties);
}

PropertyDescriptor[] propArray = (PropertyDescriptor[])propList.ToArray(typeof(PropertyDescriptor));
var propArray = new PropertyDescriptor[propList.Count];
propList.CopyTo(propArray);
_properties = new PropertyDescriptorCollection(propArray, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,9 @@ public static EventDescriptorCollection GetEvents(
ArrayList filteredEvents = FilterMembers(events, attributes);
if (filteredEvents != null)
{
events = new EventDescriptorCollection((EventDescriptor[])filteredEvents.ToArray(typeof(EventDescriptor)), true);
var descriptors = new EventDescriptor[filteredEvents.Count];
filteredEvents.CopyTo(descriptors);
events = new EventDescriptorCollection(descriptors, true);
}
}

Expand Down Expand Up @@ -1281,7 +1283,9 @@ public static PropertyDescriptorCollection GetProperties(
ArrayList filteredProperties = FilterMembers(properties, attributes);
if (filteredProperties != null)
{
properties = new PropertyDescriptorCollection((PropertyDescriptor[])filteredProperties.ToArray(typeof(PropertyDescriptor)), true);
var descriptors = new PropertyDescriptor[filteredProperties.Count];
filteredProperties.CopyTo(descriptors);
properties = new PropertyDescriptorCollection(descriptors, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
Expand Down Expand Up @@ -771,7 +771,7 @@ internal static bool IsClickOnceDeployed(AppDomain appDomain)
/// </summary>
private PropertyInfo[] SettingsFilter(PropertyInfo[] allProps)
{
ArrayList settingProps = new ArrayList();
var settingProps = new List<PropertyInfo>();
object[] attributes;
Attribute attr;

Expand All @@ -789,7 +789,7 @@ private PropertyInfo[] SettingsFilter(PropertyInfo[] allProps)
}
}

return (PropertyInfo[])settingProps.ToArray(typeof(PropertyInfo));
return settingProps.ToArray();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
Expand Down Expand Up @@ -1179,7 +1180,7 @@ private static void StaticCompletionCallback(object context, bool wasSignaled)
EventLogInternal[] interestedComponents;
lock (InternalSyncObject)
{
interestedComponents = (EventLogInternal[])info.listeningComponents.ToArray(typeof(EventLogInternal));
interestedComponents = info.listeningComponents.ToArray();
}

for (int i = 0; i < interestedComponents.Length; i++)
Expand Down Expand Up @@ -1413,7 +1414,7 @@ private sealed class LogListeningInfo
public EventLogInternal handleOwner;
public RegisteredWaitHandle registeredWaitHandle;
public WaitHandle waitHandle;
public ArrayList listeningComponents = new ArrayList();
public List<EventLogInternal> listeningComponents = new();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Runtime.InteropServices;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

namespace System.DirectoryServices.ActiveDirectory
Expand All @@ -14,7 +15,7 @@ public class DirectoryServerCollection : CollectionBase
internal readonly DirectoryContext context;
internal bool initialized;
internal readonly Hashtable? changeList;
private readonly ArrayList _copyList = new ArrayList();
private readonly List<object> _copyList = new();
private readonly DirectoryEntry? _crossRefEntry;
private readonly bool _isADAM;
private readonly bool _isForNC;
Expand Down Expand Up @@ -263,7 +264,7 @@ protected override void OnClearComplete()
{
for (int i = 0; i < _copyList.Count; i++)
{
OnRemoveComplete(i, _copyList[i]!);
OnRemoveComplete(i, _copyList[i]);
}
}
}
Expand Down Expand Up @@ -397,15 +398,15 @@ protected override void OnValidate(object value)

internal string[] GetMultiValuedProperty()
{
ArrayList values = new ArrayList();
var values = new List<string>();
for (int i = 0; i < InnerList.Count; i++)
{
DirectoryServer ds = (DirectoryServer)InnerList[i]!;

string ntdsaName = (ds is DomainController) ? ((DomainController)ds).NtdsaObjectName : ((AdamInstance)ds).NtdsaObjectName;
values.Add(ntdsaName);
}
return (string[])values.ToArray(typeof(string));
return values.ToArray();
}
}
}
26 changes: 13 additions & 13 deletions src/libraries/System.IO.Ports/tests/SerialPort/DiscardNull.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.PortsTests;
using System.Text;
Expand Down Expand Up @@ -380,8 +380,8 @@ private void VerifyDiscardNull(SerialPort com1, ReadMethodDelegate readMethod, b

private char[] Read_byte_int_int(SerialPort com)
{
ArrayList receivedBytes = new ArrayList();
byte[] buffer = new byte[DEFAULT_READ_BYTE_ARRAY_SIZE];
var receivedBytes = new List<byte>();
var buffer = new byte[DEFAULT_READ_BYTE_ARRAY_SIZE];
int totalBytesRead = 0;
int numBytes;

Expand All @@ -403,14 +403,14 @@ private char[] Read_byte_int_int(SerialPort com)
if (totalBytesRead < receivedBytes.Count)
receivedBytes.RemoveRange(totalBytesRead, receivedBytes.Count - totalBytesRead);

return com.Encoding.GetChars((byte[])receivedBytes.ToArray(typeof(byte)));
return com.Encoding.GetChars(receivedBytes.ToArray());
}


private char[] Read_char_int_int(SerialPort com)
{
ArrayList receivedChars = new ArrayList();
char[] buffer = new char[DEFAULT_READ_CHAR_ARRAY_SIZE];
var receivedChars = new List<char>();
var buffer = new char[DEFAULT_READ_CHAR_ARRAY_SIZE];
int totalCharsRead = 0;
int numChars;

Expand All @@ -432,13 +432,13 @@ private char[] Read_char_int_int(SerialPort com)
if (totalCharsRead < receivedChars.Count)
receivedChars.RemoveRange(totalCharsRead, receivedChars.Count - totalCharsRead);

return (char[])receivedChars.ToArray(typeof(char));
return receivedChars.ToArray();
}


private char[] ReadByte(SerialPort com)
{
ArrayList receivedBytes = new ArrayList();
var receivedBytes = new List<byte>();
int rcvByte;

while (true)
Expand All @@ -455,13 +455,13 @@ private char[] ReadByte(SerialPort com)
receivedBytes.Add((byte)rcvByte);
}

return com.Encoding.GetChars((byte[])receivedBytes.ToArray(typeof(byte)));
return com.Encoding.GetChars(receivedBytes.ToArray());
}


private char[] ReadChar(SerialPort com)
{
ArrayList receivedChars = new ArrayList();
var receivedChars = new List<char>();
int rcvChar;

while (true)
Expand All @@ -477,13 +477,13 @@ private char[] ReadChar(SerialPort com)
receivedChars.Add((char)rcvChar);
}

return (char[])receivedChars.ToArray(typeof(char));
return receivedChars.ToArray();
}


private char[] ReadLine(SerialPort com)
{
StringBuilder rcvStringBuilder = new StringBuilder();
var rcvStringBuilder = new StringBuilder();
string rcvString;

while (true)
Expand All @@ -506,7 +506,7 @@ private char[] ReadLine(SerialPort com)

private char[] ReadTo(SerialPort com)
{
StringBuilder rcvStringBuilder = new StringBuilder();
var rcvStringBuilder = new StringBuilder();
string rcvString;

while (true)
Expand Down
26 changes: 13 additions & 13 deletions src/libraries/System.IO.Ports/tests/SerialPort/ParityReplace.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.PortsTests;
using System.Text;
Expand Down Expand Up @@ -297,8 +297,8 @@ private void VerifyRead(SerialPort com1, SerialPort com2, byte[] bytesToWrite, c

private char[] Read_byte_int_int(SerialPort com)
{
ArrayList receivedBytes = new ArrayList();
byte[] buffer = new byte[DEFAULT_READ_BYTE_ARRAY_SIZE];
var receivedBytes = new List<byte>();
var buffer = new byte[DEFAULT_READ_BYTE_ARRAY_SIZE];
int totalBytesRead = 0;

while (true)
Expand All @@ -320,14 +320,14 @@ private char[] Read_byte_int_int(SerialPort com)
if (totalBytesRead < receivedBytes.Count)
receivedBytes.RemoveRange(totalBytesRead, receivedBytes.Count - totalBytesRead);

return com.Encoding.GetChars((byte[])receivedBytes.ToArray(typeof(byte)));
return com.Encoding.GetChars(receivedBytes.ToArray());
}


private char[] Read_char_int_int(SerialPort com)
{
ArrayList receivedChars = new ArrayList();
char[] buffer = new char[DEFAULT_READ_CHAR_ARRAY_SIZE];
var receivedChars = new List<char>();
var buffer = new char[DEFAULT_READ_CHAR_ARRAY_SIZE];
int totalCharsRead = 0;
int numChars;

Expand All @@ -349,13 +349,13 @@ private char[] Read_char_int_int(SerialPort com)
if (totalCharsRead < receivedChars.Count)
receivedChars.RemoveRange(totalCharsRead, receivedChars.Count - totalCharsRead);

return (char[])receivedChars.ToArray(typeof(char));
return receivedChars.ToArray();
}


private char[] ReadByte(SerialPort com)
{
ArrayList receivedBytes = new ArrayList();
var receivedBytes = new List<byte>();
int rcvByte;

while (true)
Expand All @@ -372,13 +372,13 @@ private char[] ReadByte(SerialPort com)
receivedBytes.Add((byte)rcvByte);
}

return com.Encoding.GetChars((byte[])receivedBytes.ToArray(typeof(byte)));
return com.Encoding.GetChars(receivedBytes.ToArray());
}


private char[] ReadChar(SerialPort com)
{
ArrayList receivedChars = new ArrayList();
var receivedChars = new List<char>();
int rcvChar;

while (true)
Expand All @@ -395,13 +395,13 @@ private char[] ReadChar(SerialPort com)
receivedChars.Add((char)rcvChar);
}

return (char[])receivedChars.ToArray(typeof(char));
return receivedChars.ToArray();
}


private char[] ReadLine(SerialPort com)
{
StringBuilder rcvStringBuilder = new StringBuilder();
var rcvStringBuilder = new StringBuilder();
string rcvString;

while (true)
Expand All @@ -424,7 +424,7 @@ private char[] ReadLine(SerialPort com)

private char[] ReadTo(SerialPort com)
{
StringBuilder rcvStringBuilder = new StringBuilder();
var rcvStringBuilder = new StringBuilder();
string rcvString;

while (true)
Expand Down
Loading

0 comments on commit c69254f

Please sign in to comment.