Repository: dotnet/aspnetcore
File: src/Components/WebAssembly/WebAssembly/src/Services/LazyAssemblyLoader.cs (C#)

Code before:
        var newAssembliesToLoad = new List<string>();
        foreach (var assemblyToLoad in assembliesToLoad)
        {
            if (!_loadedAssemblyCache.Contains(assemblyToLoad))
            {
                newAssembliesToLoad.Add(assemblyToLoad);
            }
        }

        if (newAssembliesToLoad.Count == 0)
        {
            return Array.Empty<Assembly>();
        }

        var loadedAssemblies = new List<Assembly>();
        var pendingLoads = newAssembliesToLoad.Select(assemblyToLoad => LoadAssembly(assemblyToLoad, loadedAssemblies));

        await Task.WhenAll(pendingLoads);
        return loadedAssemblies;
    }

    [RequiresUnreferencedCode("Types and members the loaded assemblies depend on might be removed")]
    [SupportedOSPlatform("browser")]
    private async Task LoadAssembly(string assemblyToLoad, List<Assembly> loadedAssemblies)
    {
        JSObject files = await LazyAssemblyLoaderInterop.LoadLazyAssembly(assemblyToLoad);

        byte[] dllBytes = files.GetPropertyAsByteArray("dll")!;
        byte[]? pdbBytes = files.GetPropertyAsByteArray("pdb");
        Assembly loadedAssembly = pdbBytes == null
                    ? AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(dllBytes))
                    : AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(dllBytes), new MemoryStream(pdbBytes));

        loadedAssemblies.Add(loadedAssembly);
        _loadedAssemblyCache!.Add(assemblyToLoad);

        files.Dispose();
    }

    private partial class LazyAssemblyLoaderInterop
    {
        [JSImport("Blazor._internal.loadLazyAssembly", "blazor-internal")]
        public static partial Task<JSObject> LoadLazyAssembly(string assemblyToLoad);
    }
}


Review comment (suggestion):
Nit:
```suggestion
        using var files = await LazyAssemblyLoaderInterop.LoadLazyAssembly(assemblyToLoad);
```

Code after:
        var newAssembliesToLoad = new List<string>();
        foreach (var assemblyToLoad in assembliesToLoad)
        {
            if (!_loadedAssemblyCache.Contains(assemblyToLoad))
            {
                newAssembliesToLoad.Add(assemblyToLoad);
            }
        }

        if (newAssembliesToLoad.Count == 0)
        {
            return Array.Empty<Assembly>();
        }

        var loadedAssemblies = new List<Assembly>();
        var pendingLoads = newAssembliesToLoad.Select(assemblyToLoad => LoadAssembly(assemblyToLoad, loadedAssemblies));

        await Task.WhenAll(pendingLoads);
        return loadedAssemblies;
    }

    [RequiresUnreferencedCode("Types and members the loaded assemblies depend on might be removed")]
    [SupportedOSPlatform("browser")]
    private async Task LoadAssembly(string assemblyToLoad, List<Assembly> loadedAssemblies)
    {
        using var files = await LazyAssemblyLoaderInterop.LoadLazyAssembly(assemblyToLoad);

        var dllBytes = files.GetPropertyAsByteArray("dll")!;
        var pdbBytes = files.GetPropertyAsByteArray("pdb");
        Assembly loadedAssembly = pdbBytes == null
                    ? AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(dllBytes))
                    : AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(dllBytes), new MemoryStream(pdbBytes));

        loadedAssemblies.Add(loadedAssembly);
        _loadedAssemblyCache!.Add(assemblyToLoad);

    }

    private partial class LazyAssemblyLoaderInterop
    {
        [JSImport("Blazor._internal.loadLazyAssembly", "blazor-internal")]
        public static partial Task<JSObject> LoadLazyAssembly(string assemblyToLoad);
    }
}