From 736cf3ecbf441af05baec72ac31bad9619a2b3e2 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 24 May 2022 11:14:39 -0700 Subject: [PATCH] Remove the assert and use a relatively larger capacity to cover possible increase of .NET reference assemblies (#17423) --- .../commands/utility/AddType.cs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index f084d96499..6cdfb512dc 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs @@ -685,11 +685,10 @@ namespace Microsoft.PowerShell.Commands /// private static IEnumerable InitDefaultRefAssemblies() { - // Define number of reference assemblies distributed with PowerShell. - const int maxPowershellRefAssemblies = 161; - - const int capacity = maxPowershellRefAssemblies + 1; - var defaultRefAssemblies = new List(capacity); + // Default reference assemblies consist of .NET reference assemblies and the 'S.M.A' assembly. + // Today, there are 161 .NET reference assemblies, so the needed capacity is 162, but we use 200 + // as the initial capacity to cover the possible increase of .NET reference assemblies in future. + var defaultRefAssemblies = new List(capacity: 200); foreach (string file in Directory.EnumerateFiles(s_netcoreAppRefFolder, "*.dll", SearchOption.TopDirectoryOnly)) { @@ -699,11 +698,6 @@ namespace Microsoft.PowerShell.Commands // Add System.Management.Automation.dll defaultRefAssemblies.Add(MetadataReference.CreateFromFile(typeof(PSObject).Assembly.Location)); - // We want to avoid reallocating the internal array, so we assert if the list capacity has increased. - Diagnostics.Assert( - defaultRefAssemblies.Capacity <= capacity, - $"defaultRefAssemblies was resized because of insufficient initial capacity! A capacity of {defaultRefAssemblies.Count} is required."); - return defaultRefAssemblies; }