Native.SourceGenerator.DependencyInjection 1.0.3

dotnet add package Native.SourceGenerator.DependencyInjection --version 1.0.3
                    
NuGet\Install-Package Native.SourceGenerator.DependencyInjection -Version 1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Native.SourceGenerator.DependencyInjection" Version="1.0.3">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Native.SourceGenerator.DependencyInjection" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="Native.SourceGenerator.DependencyInjection">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Native.SourceGenerator.DependencyInjection --version 1.0.3
                    
#r "nuget: Native.SourceGenerator.DependencyInjection, 1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Native.SourceGenerator.DependencyInjection@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Native.SourceGenerator.DependencyInjection&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=Native.SourceGenerator.DependencyInjection&version=1.0.3
                    
Install as a Cake Tool

Native.SourceGenerator.DependencyInjection

NuGet

AOT-first Source Generator for Dependency Injection - Generates constructors and service registrations without reflection.

Features

  • 100% Native AOT Compatible - No reflection, no Activator.CreateInstance
  • Compile-time Constructor Generation - All errors caught at build time
  • Automatic Service Registration - No manual services.AddSingleton<T>() calls
  • IIncrementalGenerator - Fast, incremental builds

Installation

dotnet add package Native.SourceGenerator.DependencyInjection

Usage

1. Mark your class with [Register] and fields with [Inject]

using Native.SourceGenerator.DependencyInjection;

[Register(typeof(IJwtService), ServiceLifetime.Singleton)]
public partial class JwtService : IJwtService
{
    [Inject] private readonly ISigningKeyRepository _signingKeyRepository;
    [Inject] private readonly IClockService _clockService;
    [Inject] private readonly IIdGenerator _idGenerator;
    [Inject] private readonly ILogger<JwtService> _logger;

    public string GenerateToken(User user) { /* ... */ }
}

2. Register services in Startup

var services = new ServiceCollection();
services.AddGeneratedServices();

Generated Code

The generator creates a constructor for your class:

// Auto-generated
public partial class JwtService
{
    public JwtService(
        ISigningKeyRepository signingKeyRepository,
        IClockService clockService,
        IIdGenerator idGenerator,
        ILogger<JwtService> logger)
    {
        _signingKeyRepository = signingKeyRepository;
        _clockService = clockService;
        _idGenerator = idGenerator;
        _logger = logger;
    }
}

And a registration extension method:

// Auto-generated
public static class NativeGeneratedServices
{
    public static IServiceCollection AddGeneratedServices(this IServiceCollection services)
    {
        services.AddSingleton<IJwtService, JwtService>();
        return services;
    }
}

Attributes

[Register]

Parameter Type Description
serviceType Type The interface/base type to register (optional)
lifetime ServiceLifetime Singleton, Scoped, or Transient (default: Singleton)
Group string Group name for organizing registrations (optional)

[Inject]

Marks a field for dependency injection. The field must be readonly.

Grouping Services

[Register(typeof(IAuthService), Group = "Authentication")]
public partial class AuthService : IAuthService { /* ... */ }

[Register(typeof(ITokenService), Group = "Authentication")]
public partial class TokenService : ITokenService { /* ... */ }

// In Startup:
services.AddAuthentication(); // Only adds services in "Authentication" group

Diagnostics

Code Description
DI001 Class must be partial
DI002 No injectable fields found
DI003 Injectable field must be readonly
DI005 Class must not be static
DI006 Class must not be abstract

Requirements

  • .NET 10 SDK
  • C# 13+

License

MIT

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 114 2/11/2026
1.0.2 99 2/10/2026
1.0.1 107 2/10/2026
1.0.0 109 2/10/2026