| | | 1 | | using NexusLabs.Needlr.Injection.Reflection.Loaders; |
| | | 2 | | |
| | | 3 | | using System.Diagnostics.CodeAnalysis; |
| | | 4 | | using System.Reflection; |
| | | 5 | | |
| | | 6 | | namespace NexusLabs.Needlr.Injection.Reflection; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Builder for creating assembly providers using reflection-based loaders. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// <para> |
| | | 13 | | /// This builder uses reflection-based assembly loading and is not compatible with NativeAOT or trimming. |
| | | 14 | | /// For AOT scenarios, use GeneratedAssemblyProvider from NexusLabs.Needlr.Injection.SourceGen instead. |
| | | 15 | | /// </para> |
| | | 16 | | /// <para> |
| | | 17 | | /// For assembly ordering, use <c>SyringeExtensions.OrderAssemblies</c> after configuring the Syringe. |
| | | 18 | | /// </para> |
| | | 19 | | /// </remarks> |
| | | 20 | | [RequiresUnreferencedCode("AssemblyProviderBuilder uses reflection-based assembly loading. Use GeneratedAssemblyProvider |
| | | 21 | | public sealed class AssemblyProviderBuilder : IAssemblyProviderBuilder |
| | | 22 | | { |
| | | 23 | | private IAssemblyLoader _assemblyLoader; |
| | | 24 | | |
| | 359 | 25 | | public AssemblyProviderBuilder() |
| | | 26 | | { |
| | 359 | 27 | | _assemblyLoader = new ReflectionAssemblyLoader(); |
| | 359 | 28 | | } |
| | | 29 | | |
| | | 30 | | public AssemblyProviderBuilder UseLoader(IAssemblyLoader loader) |
| | | 31 | | { |
| | 25 | 32 | | ArgumentNullException.ThrowIfNull(loader); |
| | 24 | 33 | | _assemblyLoader = loader; |
| | 24 | 34 | | return this; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | public IAssemblyProvider Build() |
| | | 38 | | { |
| | 358 | 39 | | return new AssemblyProvider(_assemblyLoader); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | private sealed class AssemblyProvider : IAssemblyProvider |
| | | 43 | | { |
| | | 44 | | private readonly Lazy<IReadOnlyList<Assembly>> _lazyAssemblies; |
| | | 45 | | |
| | 358 | 46 | | public AssemblyProvider(IAssemblyLoader assemblyLoader) |
| | | 47 | | { |
| | 358 | 48 | | ArgumentNullException.ThrowIfNull(assemblyLoader); |
| | | 49 | | |
| | 358 | 50 | | _lazyAssemblies = new(() => |
| | 358 | 51 | | { |
| | 302 | 52 | | return assemblyLoader.LoadAssemblies(continueOnAssemblyError: true); |
| | 358 | 53 | | }); |
| | 358 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | | 57 | | public IReadOnlyList<Assembly> GetCandidateAssemblies() |
| | 302 | 58 | | => _lazyAssemblies.Value; |
| | | 59 | | } |
| | | 60 | | } |