Lage.AutoInject.Generator 1.0.0

dotnet add package Lage.AutoInject.Generator --version 1.0.0
                    
NuGet\Install-Package Lage.AutoInject.Generator -Version 1.0.0
                    
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="Lage.AutoInject.Generator" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lage.AutoInject.Generator" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Lage.AutoInject.Generator" />
                    
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 Lage.AutoInject.Generator --version 1.0.0
                    
#r "nuget: Lage.AutoInject.Generator, 1.0.0"
                    
#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 Lage.AutoInject.Generator@1.0.0
                    
#: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=Lage.AutoInject.Generator&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Lage.AutoInject.Generator&version=1.0.0
                    
Install as a Cake Tool

Lage.AutoInject.Generator

NuGet: Lage.AutoInject.Generator · 仓库: GitHub · 许可证: MIT

基于 Roslyn 增量生成器的零反射 · AOT 兼容自动依赖注入工具。对实现接口的类标记 [AutoInject],编译时自动生成 AddAutoInject(this IServiceCollection) 扩展方法,入口调用一次即可批量注册所有被标记的接口。要求 .NET Core 3.0+ / .NET 5+。

安装

dotnet add package Lage.AutoInject.Generator

使用

标记实现类

using Lage.AutoInject.Core;

// 方式一:默认注册到类实现的第一个接口,生命周期 Scoped
[AutoInject]
public class Foo : IFoo { }

// 方式二:泛型版本显式指定注入接口,生命周期 Transient
[AutoInject<IBar>(ServiceLifetime.Transient)]
public class Bar : IBar { }

// 方式三:非泛型 + ServiceType 属性指定接口,生命周期 Singleton
[AutoInject(ServiceLifetime.Singleton, ServiceType = typeof(IBaz))]
public class Baz : IBaz { }

入口注册

using Lage.AutoInject; // 扩展方法命名空间
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();
services.AddAutoInject(); // 一次调用批量注册

var provider = services.BuildServiceProvider();
var foo = provider.GetRequiredService<IFoo>(); // 已注入

生成的扩展方法形如:

public static IServiceCollection AddAutoInject(this IServiceCollection services)
{
    services = AddScoped<IFoo, Foo>(services);
    services = AddTransient<IBar, Bar>(services);
    services = AddSingleton<IBaz, Baz>(services);
    return services;
}

服务接口解析优先级

  1. 泛型版本 [AutoInject<T>] → T
  2. 非泛型且设置 ServiceType 属性 → 该类型
  3. 均未指定 → 类实现的第一个接口

生命周期

LifeTime 默认 Scoped,可通过构造函数参数指定 ServiceLifetime.Singleton/Scoped/Transient。

编译诊断

ID 严重性 触发条件
AUTOINJ001 Error 标记 [AutoInject] 的类未实现任何接口且未指定服务接口
AUTOINJ002 Error 泛型参数不是接口
AUTOINJ003 Error 指定的接口与实现类无实现/继承关系
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.0 113 8/21/2026