GeneratedTypeRegistrar
NexusLabs.Needlr.Injection.SourceGen¶
NexusLabs.Needlr.Injection.SourceGen.TypeRegistrars¶
GeneratedTypeRegistrar Class¶
A type registrar that uses compile-time generated type registry instead of runtime reflection for type discovery.
Inheritance System.Object 🡒 GeneratedTypeRegistrar
Implements NexusLabs.Needlr.Injection.ITypeRegistrar
Example¶
// In your assembly (typically Program.cs or a dedicated file):
[assembly: GenerateTypeRegistry(IncludeNamespacePrefixes = new[] { "MyCompany", "NexusLabs" })]
// When building the service provider:
var serviceProvider = new Syringe()
.UsingGeneratedTypeRegistrar()
.BuildServiceProvider(config);
Remarks¶
This registrar is designed to work with the source generator from
NexusLabs.Needlr.Generators. To use it:
1. Add the NexusLabs.Needlr.Generators package to your project
2. Add [assembly: GenerateTypeRegistry(...)] attribute
3. Use .UsingGeneratedTypeRegistrar() when building the service provider
The assemblies parameter is ignored when a type provider is supplied, as all type discovery happens at compile time.
Constructors¶
GeneratedTypeRegistrar() Constructor¶
Initializes a new instance using the default generated TypeRegistry.
Remarks¶
This constructor uses reflection to locate the generated TypeRegistry class. The generated class must be present in the calling assembly.
GeneratedTypeRegistrar(Func<IReadOnlyList<InjectableTypeInfo>>) Constructor¶
Initializes a new instance with a custom type provider.
public GeneratedTypeRegistrar(System.Func<System.Collections.Generic.IReadOnlyList<NexusLabs.Needlr.Generators.InjectableTypeInfo>>? typeProvider);
Parameters¶
typeProvider System.Func<System.Collections.Generic.IReadOnlyList<NexusLabs.Needlr.Generators.InjectableTypeInfo>>
A function that returns the injectable types. If null, the registrar will attempt to locate the generated TypeRegistry at runtime.
Methods¶
GeneratedTypeRegistrar.RegisterTypesFromAssemblies(IServiceCollection, ITypeFilterer, IReadOnlyList<Assembly>) Method¶
Registers types from the specified assemblies into the service collection.
public void RegisterTypesFromAssemblies(Microsoft.Extensions.DependencyInjection.IServiceCollection services, NexusLabs.Needlr.Injection.ITypeFilterer typeFilterer, System.Collections.Generic.IReadOnlyList<System.Reflection.Assembly> assemblies);
Parameters¶
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The service collection to register types into.
typeFilterer NexusLabs.Needlr.Injection.ITypeFilterer
The type filterer used to determine which types should be registered and their lifetimes.
assemblies System.Collections.Generic.IReadOnlyList<System.Reflection.Assembly>
The assemblies to scan for types to register.
Implements RegisterTypesFromAssemblies(IServiceCollection, ITypeFilterer, IReadOnlyList<Assembly>)