diff --git a/src/componentsBase/InfragisticsBlazorExtensions.cs b/src/componentsBase/InfragisticsBlazorExtensions.cs index 1fc5519..df3c742 100644 --- a/src/componentsBase/InfragisticsBlazorExtensions.cs +++ b/src/componentsBase/InfragisticsBlazorExtensions.cs @@ -31,12 +31,15 @@ public static class InfragisticsBlazorExtensions public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddIgniteUIBlazor(this Microsoft.Extensions.DependencyInjection.IServiceCollection collection, params Type[] modulesToLoad) { + // Snapshot the caller's array immediately so any later mutation has no effect. + Type[] snapshot = modulesToLoad != null && modulesToLoad.Length > 0 ? [.. modulesToLoad] : []; + var s = collection.AddScoped( typeof(IIgniteUIBlazorSettings), (sp) => { var bs = new IgniteUIBlazorSettings(); - bs = bs.WithModulesToLoad(modulesToLoad != null && modulesToLoad.Length > 0 ? new ReadOnlyCollection(modulesToLoad) : null); + bs = bs.WithModulesToLoad(snapshot.Length > 0 ? new ReadOnlyCollection(snapshot) : null); return bs; }); diff --git a/tests/IgniteUI.Blazor.Tests/ServiceRegistrationTests.cs b/tests/IgniteUI.Blazor.Tests/ServiceRegistrationTests.cs index af251c5..5a2bbd9 100644 --- a/tests/IgniteUI.Blazor.Tests/ServiceRegistrationTests.cs +++ b/tests/IgniteUI.Blazor.Tests/ServiceRegistrationTests.cs @@ -163,6 +163,23 @@ public void Runtime_InvokesRegisterOnEveryListedModule() Assert.Same(runtime, Assert.Single(SecondModule.Registrations)); } + /// + /// Mutating the original array after calling AddIgniteUIBlazor must not change which + /// modules the settings report. The overload snapshots the array at registration time. + /// + [Fact] + public void AddIgniteUIBlazor_MutatingOriginalArray_DoesNotAffectRegisteredModules() + { + var modules = new Type[] { typeof(FirstModule) }; + + using var host = new Host(s => s.AddIgniteUIBlazor(modules)); + + // Mutate after registration, before the first resolve. + modules[0] = typeof(SecondModule); + + Assert.Equal([typeof(FirstModule)], SettingsOf(host).ModulesToLoad); + } + [Fact] public void Runtime_IsScoped_SoEachScopeRegistersItsOwn() {