惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

TaoSecurity Blog
TaoSecurity Blog
Jina AI
Jina AI
雷峰网
雷峰网
月光博客
月光博客
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
B
Blog RSS Feed
美团技术团队
C
CXSECURITY Database RSS Feed - CXSecurity.com
小众软件
小众软件
Security Latest
Security Latest
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cybersecurity and Infrastructure Security Agency CISA
Last Week in AI
Last Week in AI
A
Arctic Wolf
Latest news
Latest news
Attack and Defense Labs
Attack and Defense Labs
I
Intezer
F
Fortinet All Blogs
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog
S
Secure Thoughts
Help Net Security
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
V
Visual Studio Blog
P
Proofpoint News Feed
博客园 - 【当耐特】
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
Stack Overflow Blog
Stack Overflow Blog
Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
Hacker News: Ask HN
Hacker News: Ask HN
L
LINUX DO - 最新话题
H
Help Net Security
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tenable Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog

博客园 - 我有我奥妙

【BenchmarkDotNet】测试多方式的对象映射 【排名】处理同分数的排名 【Quartz】.Net8使用定时任务 【模型验证】未被异常捕获到 【Ant Design Vue】相关 【根节点】C#找树形数据的根节点Id 【C#】枚举值 【ECharts】图表自定义显示标题 【消息队列】介绍 【Nginx】Windows部署Vue 设计模式(一)-介绍 【.NetCore】创建本机的静态文件服务器 NLog(一)-使用示例 【nssm】windows上netcore注册为服务 【字符串排序】C#和前端js排序问题 【长路经】C#读取文件抛出FileNotFoundException异常 【RestSharp】常用的几个请求方式 【笔记软件】Obsidian的使用 【浏览器扩展】编写Firefox和Chrome的扩展程序
【自动注入】.NET8/.NETCore 依赖注入:自动注入项目中所有接口和自定义类
我有我奥妙 · 2025-04-21 · via 博客园 - 我有我奥妙

参考

https://blog.csdn.net/jonnysun/article/details/144201359

创建工具类库

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
  </ItemGroup>

</Project>

IDependency接口

    public interface IDependency { }

    public interface ITransientDependency : IDependency { }

    public interface IScopedDependency : IDependency { }

    public interface ISingletonDependency : IDependency { }

全局程序集

    public static class GlobalAssemblies
    {
        public static readonly Assembly[] AllAssemblies =
        [
            Assembly.Load("Keep.Application"),
        ];

        public static readonly Type[] AllTypes = [.. AllAssemblies.SelectMany(x => x.GetTypes())];
    }

HostExtensions扩展方法

    public static class HostExtensions
    {
        public static IServiceCollection AddInjectionServices(this IServiceCollection services)
        {
            Dictionary<Type, ServiceLifetime> lifeTimeMap = new()
            {
                 { typeof(ITransientDependency),ServiceLifetime.Transient },
                 { typeof(IScopedDependency),ServiceLifetime.Scoped },
                 { typeof(ISingletonDependency),ServiceLifetime.Singleton },
                 { typeof(IDependency),ServiceLifetime.Scoped },
             };

            var typeList = GlobalAssemblies.AllTypes;

            foreach (var type in typeList)
            {
                foreach (var map in lifeTimeMap)
                {
                    var interfaceDependency = map.Key;
                    if ((interfaceDependency.IsAssignableFrom(type) && interfaceDependency != type && !type.IsAbstract && type.IsClass) == false)
                        continue;

                    bool hasInterafce = false;
                    foreach (var @inteface in typeList.Where(x => x.IsInterface && x.IsAssignableFrom(type) && x != interfaceDependency))
                    {
                        services.Add(new ServiceDescriptor(@inteface, type, map.Value));
                        hasInterafce = true;
                    }

                    if (hasInterafce == false)
                        services.Add(new ServiceDescriptor(type, type, map.Value));
                }
            }

            return services;
        }
    }

使用Program.cs

builder.Services.AddInjectionServices();