# HardStyle.AutoDependencyInjection **Repository Path**: hardstylewyl/hardstyle.AutoDependencyInjection ## Basic Information - **Project Name**: HardStyle.AutoDependencyInjection - **Description**: 提供自动依赖注入的支持 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-02-12 - **Last Updated**: 2023-06-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: Csharp ## README # HardStyle.AutoDependencyInjection #### 介绍 提供自动依赖注入的支持 #### 使用说明 1. 将标记接口标记在想要自动注册的服务接口上 ```c#、 //标记在接口上 public interface ITestService : IScopedDependency { void Run(); } public class TestService1 : ITestService { public void Run() { Console.WriteLine("跑起来了1"); } } //直接标记在类上 public class TestService2 : IScopedDependency { public void Run() { Console.WriteLine("跑起来了2"); } } ``` 2. 在容器中使用自动依赖注入服务 ```C# builder.Services.AddAutoDependencyInjection(); ``` 3. 测试效果 控制器部分代码 ```C# private readonly ITestService _testService1; private readonly TestService2 _testService2; public TestController(ITestService testService, TestService2 testService2) { _testService1 = testService; _testService2 = testService2; } [HttpGet] public void T() { _testService1.Run(); _testService2.Run(); } ``` 调用接口方法结果 ![输入图片说明](https://foruda.gitee.com/images/1676184301917172073/be3b8d4c_8514785.png "屏幕截图") #### 喜欢此项目的话点个star吧😁😁😁😁