Removes the last usages of System.Linq in a default Blazor WASM app, which were a handful of ToArray calls.
Fix #56916
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
-using System.Linq;
using System.Reflection;
using Microsoft.Extensions.Internal;
private readonly StackGuard _stackGuard;
- public CallSiteFactory(IEnumerable<ServiceDescriptor> descriptors)
+ public CallSiteFactory(ICollection<ServiceDescriptor> descriptors)
{
_stackGuard = new StackGuard();
- _descriptors = descriptors.ToArray();
+ _descriptors = new ServiceDescriptor[descriptors.Count];
+ descriptors.CopyTo(_descriptors, 0);
+
Populate();
}
internal static bool VerifyOpenGenericServiceTrimmability { get; } =
AppContext.TryGetSwitch("Microsoft.Extensions.DependencyInjection.VerifyOpenGenericServiceTrimmability", out bool verifyOpenGenerics) ? verifyOpenGenerics : false;
- internal ServiceProvider(IEnumerable<ServiceDescriptor> serviceDescriptors, ServiceProviderOptions options)
+ internal ServiceProvider(ICollection<ServiceDescriptor> serviceDescriptors, ServiceProviderOptions options)
{
// note that Root needs to be set before calling GetEngine(), because the engine may need to access Root
Root = new ServiceProviderEngineScope(this, isRootScope: true);
ServiceDescriptor.Singleton<ICustomService, CustomService5>()
};
- var callsiteFactory = new CallSiteFactory(serviceDescriptors.Take(numberOfServices));
+ var callsiteFactory = new CallSiteFactory(serviceDescriptors.Take(numberOfServices).ToArray());
for (int i = 0; i < numberOfServices; i++)
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
-using System.Linq;
namespace Microsoft.Extensions.Options
{
/// <param name="validations">The validations to run.</param>
public OptionsFactory(IEnumerable<IConfigureOptions<TOptions>> setups, IEnumerable<IPostConfigureOptions<TOptions>> postConfigures, IEnumerable<IValidateOptions<TOptions>> validations)
{
- _setups = setups as IConfigureOptions<TOptions>[] ?? setups.ToArray();
- _postConfigures = postConfigures as IPostConfigureOptions<TOptions>[] ?? postConfigures.ToArray();
- _validations = validations as IValidateOptions<TOptions>[] ?? validations.ToArray();
+ // The default DI container uses arrays under the covers. Take advantage of this knowledge
+ // by checking for an array and enumerate over that, so we don't need to allocate an enumerator.
+ // When it isn't already an array, convert it to one, but don't use System.Linq to avoid pulling Linq in to
+ // small trimmed applications.
+
+ _setups = setups as IConfigureOptions<TOptions>[] ?? new List<IConfigureOptions<TOptions>>(setups).ToArray();
+ _postConfigures = postConfigures as IPostConfigureOptions<TOptions>[] ?? new List<IPostConfigureOptions<TOptions>>(postConfigures).ToArray();
+ _validations = validations as IValidateOptions<TOptions>[] ?? new List<IValidateOptions<TOptions>>(validations).ToArray();
}
/// <summary>
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
-using System.Linq;
using Microsoft.Extensions.Primitives;
namespace Microsoft.Extensions.Options
_factory = factory;
_cache = cache;
- foreach (IOptionsChangeTokenSource<TOptions> source in (sources as IOptionsChangeTokenSource<TOptions>[] ?? sources.ToArray()))
+ void RegisterSource(IOptionsChangeTokenSource<TOptions> source)
{
IDisposable registration = ChangeToken.OnChange(
- () => source.GetChangeToken(),
- (name) => InvokeChanged(name),
- source.Name);
+ () => source.GetChangeToken(),
+ (name) => InvokeChanged(name),
+ source.Name);
_registrations.Add(registration);
}
+
+ // The default DI container uses arrays under the covers. Take advantage of this knowledge
+ // by checking for an array and enumerate over that, so we don't need to allocate an enumerator.
+ if (sources is IOptionsChangeTokenSource<TOptions>[] sourcesArray)
+ {
+ foreach (IOptionsChangeTokenSource<TOptions> source in sourcesArray)
+ {
+ RegisterSource(source);
+ }
+ }
+ else
+ {
+ foreach (IOptionsChangeTokenSource<TOptions> source in sources)
+ {
+ RegisterSource(source);
+ }
+ }
}
private void InvokeChanged(string name)