MFToolkit.AutoGenerator 1.0.20

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

MFToolkit.AutoGenerator - 自动注入源码生成器

概述

基于 Roslyn 源码生成器技术,在编译时自动扫描标记了注入特性的类,并生成对应的依赖注入代码。仅支持 .NET 9.0 及以上版本。

安装要求


<PackageReference Include="MFToolkit.AutoGenerator" Version="1.0.20" />
<PackageReference Include="MFToolkit.Abstractions" Version="1.0.20" />

生成结果示例

输入代码

[Singleton<IMyService>]
public class MyService : IMyService { }

[Scoped<IRepository>("db1")]
public class Repository : IRepository { }

自动生成的代码

// AutoGenerated/AutoInject.g.cs
public static partial class AutoInjectExtensions
{
    public static IServiceCollection AddAutoInjectServices(this IServiceCollection services)
    {
        // Singleton 服务注册
        services.AddSingleton<IMyService, MyService>();

        // Scoped 服务注册
        services.AddKeyedScoped<IRepository, Repository>("db1");

        return services;
    }
}

使用方法

1. 在 Startup 或 Program 中调用

var builder = WebApplication.CreateBuilder(args);

// 调用自动生成的扩展方法
builder.Services.AddAutoInjectServices();

var app = builder.Build();

2. 使用自定义服务名称

// 如果使用了 AutoInjectServiceName 特性
builder.Services.AddMyCustomServices(); // 自定义的方法名

支持的功能

  • ✅ 多种生命周期(Singleton、Scoped、Transient)
  • ✅ Keyed Services 支持
  • ✅ TryAdd 模式防止重复注册
  • ✅ 泛型服务注册
  • ✅ 自定义扩展方法名称
  • ✅ 命名空间分组生成

注意事项

  • 编译时生成 - 生成的代码在编译时产生,不会污染源代码
  • 性能优化 - 使用 Incremental Generator 技术,增量编译时只处理变化的代码
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.20 324 11/12/2025
1.0.19 207 10/15/2025
1.0.18 194 10/10/2025
1.0.17 219 6/24/2025
1.0.16 199 6/24/2025
1.0.15 215 6/24/2025
1.0.13 199 6/24/2025
1.0.12 210 6/3/2025
1.0.10 205 6/2/2025
1.0.9 261 4/17/2025
1.0.8 245 4/16/2025
1.0.7 248 4/16/2025
1.0.6 258 4/16/2025
1.0.5 276 4/12/2025 1.0.5 is deprecated because it has critical bugs.
1.0.3 252 3/9/2025
1.0.2 372 3/5/2025 1.0.2 is deprecated because it has critical bugs.
1.0.1 377 3/5/2025 1.0.1 is deprecated because it has critical bugs.
1.0.0 378 3/4/2025 1.0.0 is deprecated because it has critical bugs.
0.20.0-beta 263 3/4/2025
0.19.0-beta 260 3/4/2025
Loading failed