From f9a8abc8a5d9981e25f898181e734dc77c7e15e4 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Fri, 20 Nov 2020 18:58:31 -0500 Subject: [PATCH] Remove more LINQ usage from various dotnet/runtime libraries (#44964) * Remove unnecessary OrderBy / copy from MemoryCache.Compact * Replace First() in ILEmitResolverBuilder with [0] * Remove stale "using System.Linq;" from System.Text.Json * Remove stale "using System.Linq;" from System.Security.Cryptography.X509Certificates * Remove some LINQ usage from System.Security.Cryptography.Pkcs * Remove System.Linq reference from System.Private.Xml * Remove System.Linq reference from System.Net.WebHeaderCollection * Remove stale "using System.Linq;" from CookieContainer * Remove stale "using System.Linq;" from AltSvcHeaderParser * Remove Enumerable.Contains on a string from System.IO.Packaging * Remove System.Linq dependency from System.Diagnostics.Process * Remove LINQ usage from Microsoft.Extensions.Options * Remove LINQ usage from Microsoft.Extensions.Logging * Remove LINQ usage from Microsoft.Extensions.Logging.Console * Remove LINQ from CollectionExtensions.GetAssets/RuntimeFiles * Update src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/Rfc3161TimestampToken.cs Co-authored-by: Jeremy Barton Co-authored-by: Jeremy Barton --- .../src/MemoryCache.cs | 50 ++++++++-------- .../ILEmit/ILEmitResolverBuilder.cs | 3 +- .../src/CollectionExtensions.cs | 27 +++++++-- .../src/ConsoleLoggerProvider.cs | 32 +++++----- .../src/JsonConsoleFormatter.cs | 4 -- .../src/LoggerFactory.cs | 3 +- .../src/DataAnnotationValidateOptions.cs | 2 - .../src/OptionsServiceCollectionExtensions.cs | 60 ++++++++++++------- .../src/System.Diagnostics.Process.csproj | 1 - .../Diagnostics/ProcessManager.Linux.cs | 12 +--- .../Packaging/PackUriHelper.PackUriScheme.cs | 3 +- .../Net/Http/Headers/AltSvcHeaderParser.cs | 6 -- .../src/System/Net/CookieContainer.cs | 2 - .../src/System.Net.WebHeaderCollection.csproj | 1 - .../src/System/Net/HeaderInfoTable.cs | 1 - .../src/System.Private.Xml.csproj | 1 - .../src/System/Xml/Serialization/Compiler.cs | 24 +++----- .../ReflectionXmlSerializationWriter.cs | 4 -- .../Xml/Serialization/SoapAttributes.cs | 9 +-- .../System/Xml/Serialization/XmlAttributes.cs | 11 +--- .../XmlSerializationEventSource.cs | 5 -- .../System/Xml/Xslt/XslCompiledTransform.cs | 4 -- .../Pkcs/Rfc3161TimestampRequest.cs | 8 ++- .../Pkcs/Rfc3161TimestampToken.cs | 35 ++++++----- .../Pkcs/Rfc3161TimestampTokenInfo.cs | 8 ++- .../Pal.Unix/CachedSystemStoreProvider.cs | 1 - .../ReflectionEmitMemberAccessor.cs | 1 - 27 files changed, 150 insertions(+), 168 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Caching.Memory/src/MemoryCache.cs b/src/libraries/Microsoft.Extensions.Caching.Memory/src/MemoryCache.cs index 55dd1217e65f9..83de60b556abc 100644 --- a/src/libraries/Microsoft.Extensions.Caching.Memory/src/MemoryCache.cs +++ b/src/libraries/Microsoft.Extensions.Caching.Memory/src/MemoryCache.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Linq; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; @@ -451,35 +450,36 @@ private void Compact(long removalSizeTarget, Func computeEntry { RemoveEntry(entry); } - } - /// Policy: - /// 1. Least recently used objects. - /// ?. Items with the soonest absolute expiration. - /// ?. Items with the soonest sliding expiration. - /// ?. Larger objects - estimated by object graph size, inaccurate. - private void ExpirePriorityBucket(ref long removedSize, long removalSizeTarget, Func computeEntrySize, List entriesToRemove, List priorityEntries) - { - // Do we meet our quota by just removing expired entries? - if (removalSizeTarget <= removedSize) + // Policy: + // 1. Least recently used objects. + // ?. Items with the soonest absolute expiration. + // ?. Items with the soonest sliding expiration. + // ?. Larger objects - estimated by object graph size, inaccurate. + static void ExpirePriorityBucket(ref long removedSize, long removalSizeTarget, Func computeEntrySize, List entriesToRemove, List priorityEntries) { - // No-op, we've met quota - return; - } - - // Expire enough entries to reach our goal - // TODO: Refine policy + // Do we meet our quota by just removing expired entries? + if (removalSizeTarget <= removedSize) + { + // No-op, we've met quota + return; + } - // LRU - foreach (CacheEntry entry in priorityEntries.OrderBy(entry => entry.LastAccessed)) - { - entry.SetExpired(EvictionReason.Capacity); - entriesToRemove.Add(entry); - removedSize += computeEntrySize(entry); + // Expire enough entries to reach our goal + // TODO: Refine policy - if (removalSizeTarget <= removedSize) + // LRU + priorityEntries.Sort((e1, e2) => e1.LastAccessed.CompareTo(e2.LastAccessed)); + foreach (CacheEntry entry in priorityEntries) { - break; + entry.SetExpired(EvictionReason.Capacity); + entriesToRemove.Add(entry); + removedSize += computeEntrySize(entry); + + if (removalSizeTarget <= removedSize) + { + break; + } } } } diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ILEmit/ILEmitResolverBuilder.cs b/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ILEmit/ILEmitResolverBuilder.cs index e053565832f9e..41370e45aeefa 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ILEmit/ILEmitResolverBuilder.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ILEmit/ILEmitResolverBuilder.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Linq; using System.Reflection; using System.Reflection.Emit; @@ -19,7 +18,7 @@ internal sealed class ILEmitResolverBuilder : CallSiteVisitor self) => GetGroup(self, string.Empty); + public static RuntimeAssetGroup GetRuntimeGroup(this IEnumerable self, string runtime) { if (string.IsNullOrEmpty(runtime)) @@ -35,9 +36,16 @@ public static IEnumerable GetRuntimeAssets(this IEnumerable GetAssets(IEnumerable groups, string runtime) { - return groups - .Where(a => string.Equals(a.Runtime, runtime, StringComparison.Ordinal)) - .SelectMany(a => a.AssetPaths); + foreach (RuntimeAssetGroup group in groups) + { + if (group.Runtime == runtime) + { + foreach (string path in group.AssetPaths) + { + yield return path; + } + } + } } public static IEnumerable GetDefaultRuntimeFileAssets(this IEnumerable self) => GetRuntimeFiles(self, string.Empty); @@ -52,9 +60,16 @@ public static IEnumerable GetRuntimeFileAssets(this IEnumerable GetRuntimeFiles(IEnumerable groups, string runtime) { - return groups - .Where(a => string.Equals(a.Runtime, runtime, StringComparison.Ordinal)) - .SelectMany(a => a.RuntimeFiles); + foreach (RuntimeAssetGroup group in groups) + { + if (group.Runtime == runtime) + { + foreach (RuntimeFile file in group.RuntimeFiles) + { + yield return file; + } + } + } } } } diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs index d5a22520a1e29..7a8f61301b475 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Linq; using System.Runtime.InteropServices; using Microsoft.Extensions.Options; @@ -29,7 +28,7 @@ public class ConsoleLoggerProvider : ILoggerProvider, ISupportExternalScope /// /// The options to create instances with. public ConsoleLoggerProvider(IOptionsMonitor options) - : this(options, Enumerable.Empty()) { } + : this(options, Array.Empty()) { } /// /// Creates an instance of . @@ -76,23 +75,26 @@ private static bool DoesConsoleSupportAnsi() private void SetFormatters(IEnumerable formatters = null) { - _formatters = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); - if (formatters == null || !formatters.Any()) - { - var defaultMonitor = new FormatterOptionsMonitor(new SimpleConsoleFormatterOptions()); - var systemdMonitor = new FormatterOptionsMonitor(new ConsoleFormatterOptions()); - var jsonMonitor = new FormatterOptionsMonitor(new JsonConsoleFormatterOptions()); - _formatters.GetOrAdd(ConsoleFormatterNames.Simple, formatterName => new SimpleConsoleFormatter(defaultMonitor)); - _formatters.GetOrAdd(ConsoleFormatterNames.Systemd, formatterName => new SystemdConsoleFormatter(systemdMonitor)); - _formatters.GetOrAdd(ConsoleFormatterNames.Json, formatterName => new JsonConsoleFormatter(jsonMonitor)); - } - else + var cd = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); + + bool added = false; + if (formatters != null) { foreach (ConsoleFormatter formatter in formatters) { - _formatters.GetOrAdd(formatter.Name, formatterName => formatter); + cd.TryAdd(formatter.Name, formatter); + added = true; } } + + if (!added) + { + cd.TryAdd(ConsoleFormatterNames.Simple, new SimpleConsoleFormatter(new FormatterOptionsMonitor(new SimpleConsoleFormatterOptions()))); + cd.TryAdd(ConsoleFormatterNames.Systemd, new SystemdConsoleFormatter(new FormatterOptionsMonitor(new ConsoleFormatterOptions()))); + cd.TryAdd(ConsoleFormatterNames.Json, new JsonConsoleFormatter(new FormatterOptionsMonitor(new JsonConsoleFormatterOptions()))); + } + + _formatters = cd; } // warning: ReloadLoggerOptions can be called before the ctor completed,... before registering all of the state used in this method need to be initialized @@ -135,7 +137,7 @@ public ILogger CreateLogger(string name) { UpdateFormatterOptions(logFormatter, _options.CurrentValue); } -#pragma warning disable CS0618 +#pragma warning restore CS0618 } return _loggers.GetOrAdd(name, loggerName => new ConsoleLogger(name, _messageQueue) diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs index 41cd6e32336f0..c08038c5df9aa 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs @@ -2,16 +2,12 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; -using System.Buffers; using System.Collections.Generic; using System.Globalization; using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; using System.Text.Json; -using System.Text.Json.Serialization; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; diff --git a/src/libraries/Microsoft.Extensions.Logging/src/LoggerFactory.cs b/src/libraries/Microsoft.Extensions.Logging/src/LoggerFactory.cs index 86615b4db06c1..3ee0e0f07f1d8 100644 --- a/src/libraries/Microsoft.Extensions.Logging/src/LoggerFactory.cs +++ b/src/libraries/Microsoft.Extensions.Logging/src/LoggerFactory.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; -using System.Linq; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; @@ -28,7 +27,7 @@ public class LoggerFactory : ILoggerFactory /// /// Creates a new instance. /// - public LoggerFactory() : this(Enumerable.Empty()) + public LoggerFactory() : this(Array.Empty()) { } diff --git a/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/DataAnnotationValidateOptions.cs b/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/DataAnnotationValidateOptions.cs index 2b905258fa787..5b6988ca69311 100644 --- a/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/DataAnnotationValidateOptions.cs +++ b/src/libraries/Microsoft.Extensions.Options.DataAnnotations/src/DataAnnotationValidateOptions.cs @@ -1,10 +1,8 @@ // 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.Generic; using System.ComponentModel.DataAnnotations; -using System.Linq; namespace Microsoft.Extensions.Options { diff --git a/src/libraries/Microsoft.Extensions.Options/src/OptionsServiceCollectionExtensions.cs b/src/libraries/Microsoft.Extensions.Options/src/OptionsServiceCollectionExtensions.cs index ca10c35507bf1..d9ff1e75b035b 100644 --- a/src/libraries/Microsoft.Extensions.Options/src/OptionsServiceCollectionExtensions.cs +++ b/src/libraries/Microsoft.Extensions.Options/src/OptionsServiceCollectionExtensions.cs @@ -4,8 +4,6 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Linq; -using System.Reflection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; @@ -146,28 +144,29 @@ public static IServiceCollection PostConfigureAll(this IServiceCollect where TConfigureOptions : class => services.ConfigureOptions(typeof(TConfigureOptions)); - private static bool IsAction(Type type) - => (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Action<>)); - private static IEnumerable FindConfigurationServices(Type type) { - IEnumerable serviceTypes = type - .GetInterfaces() - .Where(t => t.IsGenericType) - .Where(t => - t.GetGenericTypeDefinition() == typeof(IConfigureOptions<>) || - t.GetGenericTypeDefinition() == typeof(IPostConfigureOptions<>) || - t.GetGenericTypeDefinition() == typeof(IValidateOptions<>)); - if (!serviceTypes.Any()) + foreach (Type t in type.GetInterfaces()) { - throw new InvalidOperationException( - IsAction(type) - ? SR.Error_NoConfigurationServicesAndAction - : SR.Error_NoConfigurationServices); + if (t.IsGenericType) + { + Type gtd = t.GetGenericTypeDefinition(); + if (gtd == typeof(IConfigureOptions<>) || + gtd == typeof(IPostConfigureOptions<>) || + gtd == typeof(IValidateOptions<>)) + { + yield return t; + } + } } - return serviceTypes; } + private static void ThrowNoConfigServices(Type type) => + throw new InvalidOperationException( + type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Action<>) ? + SR.Error_NoConfigurationServicesAndAction : + SR.Error_NoConfigurationServices); + /// /// Registers a type that will have all of its , /// , and @@ -181,11 +180,19 @@ public static IServiceCollection ConfigureOptions( [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type configureType) { services.AddOptions(); - IEnumerable serviceTypes = FindConfigurationServices(configureType); - foreach (Type serviceType in serviceTypes) + + bool added = false; + foreach (Type serviceType in FindConfigurationServices(configureType)) { services.AddTransient(serviceType, configureType); + added = true; + } + + if (!added) + { + ThrowNoConfigServices(configureType); } + return services; } @@ -200,11 +207,20 @@ public static IServiceCollection ConfigureOptions( public static IServiceCollection ConfigureOptions(this IServiceCollection services, object configureInstance) { services.AddOptions(); - IEnumerable serviceTypes = FindConfigurationServices(configureInstance.GetType()); - foreach (Type serviceType in serviceTypes) + Type configureType = configureInstance.GetType(); + + bool added = false; + foreach (Type serviceType in FindConfigurationServices(configureType)) { services.AddSingleton(serviceType, configureInstance); + added = true; + } + + if (!added) + { + ThrowNoConfigServices(configureType); } + return services; } diff --git a/src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj b/src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj index c7f5c572c1bf2..3c41c1027ffc5 100644 --- a/src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj +++ b/src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj @@ -339,7 +339,6 @@ - diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Linux.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Linux.cs index 01461e9b4f061..957d0746cc9cb 100644 --- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Linux.cs +++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Linux.cs @@ -4,18 +4,13 @@ using System.Collections.Generic; using System.Globalization; using System.IO; -using System.Linq; -using System.Text; namespace System.Diagnostics { internal static partial class ProcessManager { /// Gets the IDs of all processes on the current machine. - public static int[] GetProcessIds() - { - return EnumerateProcessIds().ToArray(); - } + public static int[] GetProcessIds() => new List(EnumerateProcessIds()).ToArray(); /// Gets process infos for each process on the specified machine. /// The target machine. @@ -23,11 +18,10 @@ public static int[] GetProcessIds() public static ProcessInfo[] GetProcessInfos(string machineName) { ThrowIfRemoteMachine(machineName); - int[] procIds = GetProcessIds(machineName); // Iterate through all process IDs to load information about each process - var processes = new List(procIds.Length); - foreach (int pid in procIds) + var processes = new List(); + foreach (int pid in EnumerateProcessIds()) { ProcessInfo? pi = CreateProcessInfo(pid); if (pi != null) diff --git a/src/libraries/System.IO.Packaging/src/System/IO/Packaging/PackUriHelper.PackUriScheme.cs b/src/libraries/System.IO.Packaging/src/System/IO/Packaging/PackUriHelper.PackUriScheme.cs index 5a6558e28031d..b9b0fc231f2df 100644 --- a/src/libraries/System.IO.Packaging/src/System/IO/Packaging/PackUriHelper.PackUriScheme.cs +++ b/src/libraries/System.IO.Packaging/src/System/IO/Packaging/PackUriHelper.PackUriScheme.cs @@ -3,7 +3,6 @@ using System.Diagnostics; using System.Text; -using System.Linq; namespace System.IO.Packaging { @@ -286,7 +285,7 @@ private static string EscapeSpecialCharacters(string path) // This is currently enforced by the order of characters in the s_specialCharacterChars array foreach (char c in s_specialCharacterChars) { - if (path.Contains(c)) + if (path.IndexOf(c) != -1) { path = path.Replace(c.ToString(), Uri.HexEscape(c)); } diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/AltSvcHeaderParser.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/AltSvcHeaderParser.cs index 35c98f80b66e3..f6c65298d179a 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/AltSvcHeaderParser.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/AltSvcHeaderParser.cs @@ -1,15 +1,9 @@ // 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.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; -using System.Linq; -using System.Resources; using System.Text; -using System.Threading; -using System.Threading.Tasks; namespace System.Net.Http.Headers { diff --git a/src/libraries/System.Net.Primitives/src/System/Net/CookieContainer.cs b/src/libraries/System.Net.Primitives/src/System/Net/CookieContainer.cs index 9728030b8cd4d..e64873c04f61c 100644 --- a/src/libraries/System.Net.Primitives/src/System/Net/CookieContainer.cs +++ b/src/libraries/System.Net.Primitives/src/System/Net/CookieContainer.cs @@ -5,8 +5,6 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics; -using System.IO; -using System.Linq; using System.Net.NetworkInformation; using System.Text; diff --git a/src/libraries/System.Net.WebHeaderCollection/src/System.Net.WebHeaderCollection.csproj b/src/libraries/System.Net.WebHeaderCollection/src/System.Net.WebHeaderCollection.csproj index 3d060d6ebc3c6..d52760c4c646b 100644 --- a/src/libraries/System.Net.WebHeaderCollection/src/System.Net.WebHeaderCollection.csproj +++ b/src/libraries/System.Net.WebHeaderCollection/src/System.Net.WebHeaderCollection.csproj @@ -23,7 +23,6 @@ - diff --git a/src/libraries/System.Net.WebHeaderCollection/src/System/Net/HeaderInfoTable.cs b/src/libraries/System.Net.WebHeaderCollection/src/System/Net/HeaderInfoTable.cs index c0a7f92ac8e15..4e23fcc418aee 100644 --- a/src/libraries/System.Net.WebHeaderCollection/src/System/Net/HeaderInfoTable.cs +++ b/src/libraries/System.Net.WebHeaderCollection/src/System/Net/HeaderInfoTable.cs @@ -4,7 +4,6 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics; -using System.Linq; namespace System.Net { diff --git a/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj b/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj index cd4f3919f6e5e..89f0879720374 100644 --- a/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj +++ b/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj @@ -562,7 +562,6 @@ - diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/Compiler.cs b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/Compiler.cs index 3892522ed51e4..890628ad68afc 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/Compiler.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/Compiler.cs @@ -1,25 +1,15 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Reflection; +using System.Collections; +using System.IO; +using System.Diagnostics; +using System.Globalization; +using System.Runtime.CompilerServices; + namespace System.Xml.Serialization { - using System.Reflection; - using System.Reflection.Emit; - using System.Collections; - using System.IO; - using System; - using System.Text; - using System.ComponentModel; - using System.Security; - using System.Diagnostics; - using System.Threading; - using System.Xml.Serialization.Configuration; - using System.Globalization; - using System.Runtime.Versioning; - using System.Runtime.CompilerServices; - using System.Collections.Generic; - using System.Linq; - internal class Compiler { private readonly StringWriter _writer = new StringWriter(CultureInfo.InvariantCulture); diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationWriter.cs b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationWriter.cs index 1bbf66d2ba940..96fbd5a7ad3b9 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationWriter.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationWriter.cs @@ -1,16 +1,12 @@ // 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.Diagnostics; -using System.Linq; using System.Reflection; using System.Text; -using System.Threading.Tasks; using System.Xml.Schema; -using System.Xml; namespace System.Xml.Serialization { diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/SoapAttributes.cs b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/SoapAttributes.cs index 8f7faee01d1c2..b3c9409becd08 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/SoapAttributes.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/SoapAttributes.cs @@ -1,14 +1,11 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Reflection; +using System.ComponentModel; + namespace System.Xml.Serialization { - using System; - using System.Reflection; - using System.Collections; - using System.ComponentModel; - using System.Linq; - internal enum SoapAttributeFlags { Enum = 0x1, diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlAttributes.cs b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlAttributes.cs index f8fbd5e4e246e..06c999cc2b86a 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlAttributes.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlAttributes.cs @@ -1,16 +1,11 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Reflection; +using System.ComponentModel; + namespace System.Xml.Serialization { - using System; - using System.Reflection; - using System.Collections; - using System.ComponentModel; - using System.Linq; - using System.Collections.Generic; - using System.Xml.Serialization; - internal enum XmlAttributeFlags { Enum = 0x1, diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationEventSource.cs b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationEventSource.cs index 605cc23c4d171..d9fe51c556258 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationEventSource.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationEventSource.cs @@ -1,11 +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.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Diagnostics.Tracing; namespace System.Xml.Serialization diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Xslt/XslCompiledTransform.cs b/src/libraries/System.Private.Xml/src/System/Xml/Xslt/XslCompiledTransform.cs index df1a0223c41af..f90aadcb9eb70 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Xslt/XslCompiledTransform.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Xslt/XslCompiledTransform.cs @@ -10,14 +10,10 @@ using System.IO; using System.Reflection; using System.Reflection.Emit; -using System.Security; using System.Xml.XPath; using System.Xml.Xsl.Qil; using System.Xml.Xsl.Runtime; using System.Xml.Xsl.Xslt; -using System.Runtime.Versioning; -using System.Collections.Generic; -using System.Linq; namespace System.Xml.Xsl { diff --git a/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/Rfc3161TimestampRequest.cs b/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/Rfc3161TimestampRequest.cs index 0a66a52deb3e5..e2f30dd54bb77 100644 --- a/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/Rfc3161TimestampRequest.cs +++ b/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/Rfc3161TimestampRequest.cs @@ -4,7 +4,6 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Formats.Asn1; -using System.Linq; using System.Security.Cryptography.Asn1; using System.Security.Cryptography.Pkcs.Asn1; using System.Security.Cryptography.X509Certificates; @@ -317,8 +316,11 @@ public static Rfc3161TimestampRequest CreateFromHash( if (extensions != null) { - req.Extensions = - extensions.OfType().Select(e => new X509ExtensionAsn(e)).ToArray(); + req.Extensions = new X509ExtensionAsn[extensions.Count]; + for (int i = 0; i < extensions.Count; i++) + { + req.Extensions[i] = new X509ExtensionAsn(extensions[i]); + } } // The RFC implies DER (see TryParse), and DER is the most widely understood given that diff --git a/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/Rfc3161TimestampToken.cs b/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/Rfc3161TimestampToken.cs index e1f461d9e7874..7d0d6b08cc50f 100644 --- a/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/Rfc3161TimestampToken.cs +++ b/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/Rfc3161TimestampToken.cs @@ -4,7 +4,6 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Formats.Asn1; -using System.Linq; using System.Security.Cryptography.Asn1; using System.Security.Cryptography.Asn1.Pkcs7; using System.Security.Cryptography.Pkcs.Asn1; @@ -241,14 +240,21 @@ private static bool CheckCertificate( // // id-kp-timeStamping. This extension MUST be critical. - using (var ekuExts = tsaCertificate.Extensions.OfType().GetEnumerator()) + X509ExtensionCollection extensions = tsaCertificate.Extensions; + bool anyFound = false; + for (int i = 0; i < extensions.Count; i++) { - if (!ekuExts.MoveNext()) + if (extensions[i] is not X509EnhancedKeyUsageExtension ekuExt) + { + continue; + } + + if (anyFound) { return false; } - X509EnhancedKeyUsageExtension ekuExt = ekuExts.Current; + anyFound = true; if (!ekuExt.Critical) { @@ -270,22 +276,21 @@ private static bool CheckCertificate( { return false; } + } - if (ekuExts.MoveNext()) + if (anyFound) + { + try + { + signer.CheckSignature(new X509Certificate2Collection(tsaCertificate), true); + return true; + } + catch (CryptographicException) { - return false; } } - try - { - signer.CheckSignature(new X509Certificate2Collection(tsaCertificate), true); - return true; - } - catch (CryptographicException) - { - return false; - } + return false; } public static bool TryDecode(ReadOnlyMemory encodedBytes, [NotNullWhen(true)] out Rfc3161TimestampToken? token, out int bytesConsumed) diff --git a/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/Rfc3161TimestampTokenInfo.cs b/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/Rfc3161TimestampTokenInfo.cs index bd514ccb91f84..f3e70b14d2a16 100644 --- a/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/Rfc3161TimestampTokenInfo.cs +++ b/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/Rfc3161TimestampTokenInfo.cs @@ -4,7 +4,6 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Formats.Asn1; -using System.Linq; using System.Security.Cryptography.Asn1; using System.Security.Cryptography.Pkcs.Asn1; using System.Security.Cryptography.X509Certificates; @@ -362,8 +361,11 @@ private static byte[] Encode( if (extensions != null) { - tstInfo.Extensions = extensions.OfType(). - Select(ex => new X509ExtensionAsn(ex)).ToArray(); + tstInfo.Extensions = new X509ExtensionAsn[extensions.Count]; + for (int i = 0; i < extensions.Count; i++) + { + tstInfo.Extensions[i] = new X509ExtensionAsn(extensions[i]); + } } AsnWriter writer = new AsnWriter(AsnEncodingRules.DER); diff --git a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CachedSystemStoreProvider.cs b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CachedSystemStoreProvider.cs index b3d7ce1d1a8fc..d78e648a00050 100644 --- a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CachedSystemStoreProvider.cs +++ b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CachedSystemStoreProvider.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; -using System.Linq; using System.Runtime.InteropServices; using System.Security.Cryptography.X509Certificates; using System.Threading; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionEmitMemberAccessor.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionEmitMemberAccessor.cs index 72ba531b78612..6dc8490a398f0 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionEmitMemberAccessor.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionEmitMemberAccessor.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; -using System.Linq; using System.Reflection; using System.Reflection.Emit;