# dotnet_ioc_generator **Repository Path**: dotnetydd_com/dotnet_ioc_generator ## Basic Information - **Project Name**: dotnet_ioc_generator - **Description**: dotnet 依赖注入生成器(SourceGenerator实现) - **Primary Language**: Unknown - **License**: GPL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-01-18 - **Last Updated**: 2024-01-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 依赖注入代码自动生成器 1. [原生注入代码生成](https://github.com/pluto-arch/Dncy.Microsoft.DependencyInjection.Generator/blob/main/Dncy.DependencyInjection.Generator/AutoInject_README.md) ### Nuget包 [![](https://img.shields.io/nuget/v/Dncy.DependencyInjection.Generator.svg)](https://www.nuget.org/packages/Dncy.DependencyInjection.Generator) ### 注入使用示例: 测试项目 ConsoleTest ``` [Injectable(InjectLifeTime.Scoped)] public class Demo { public void Cw() { Console.WriteLine("Demo"); } } [Injectable(InjectLifeTime.Scoped, typeof(IServiceA))] public class ServiceA:IServiceA { public void Cw() { Console.WriteLine("IServiceA"); } } // 以上两个都会有被注入 var services = new ServiceCollection(); services.AutoInjectConsoleTest(); // 这里扩展名称是:AutoInject+程序集名称方式 ``` 2. [原生注入解析代码生成](https://github.com/pluto-arch/Dncy.Microsoft.DependencyInjection.Generator/blob/main/Dncy.DependencyInjection.Generator/ConstructorResolve_README.md) ### 构造函数解析使用示例: ``` [ApiController] [AutoResolveDependency] [Route("[controller]")] public partial class ValueController:ControllerBase { [AutoInject] private readonly IBaseRepository _baseRepository; [AutoInject] private readonly ICustomeRepository _customeRepository; [HttpGet] public IEnumerable Get() { var weatherForecasts = _baseRepository.GetList(); return new List() {"123"}; } } ``` > 支持类继承的解析 ## 源码调试 在调试的时候,目标项目需要引入生成器先项目,然后再 csproj 中加入: ``` ``` 然后在生成器的`Initialize`方法中使用`Debugger` ``` if (!Debugger.IsAttached) { Debugger.Launch(); } ``` > 调试的时候 可能更改完不会生成最新的目标代码,需要重启 vs,重新打开项目。 > 打包后引入就不会出现这种问题了。 ## 打包 对应项目的 Csproj 中需要添加一下节点配置 ``` ```