成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

精準掌握.NET依賴注入:DI自動注冊服務輕松搞定

開發 后端
在.NET中,進行依賴注入(DI)的自動注冊,可以通過反射機制和程序集掃描來實現。以下是詳細的步驟以及相應的C#源代碼示例,包括注冊指定類、注冊帶有自定義特性的類、以及注冊項目下所有帶有接口實現的類(項目下的所有接口)。

概述:.NET依賴注入(DI)通過反射自動注冊服務,示例展示了注冊指定類、帶特性類、項目下所有接口實現的類。簡化配置,提高可維護性。

在.NET中,進行依賴注入(DI)的自動注冊,可以通過反射機制和程序集掃描來實現。以下是詳細的步驟以及相應的C#源代碼示例,包括注冊指定類、注冊帶有自定義特性的類、以及注冊項目下所有帶有接口實現的類(項目下的所有接口):

步驟1:創建接口和實現類

// 接口1
public interface IService1
{
    void PerformService1();
}

// 接口2
public interface IService2
{
    void PerformService2();
}

// 實現類1,實現IService1
public class MyService1 : IService1
{
    public void PerformService1()
    {
        Console.WriteLine("Service 1 performed.");
    }
}

// 實現類2,實現IService2
[CustomRegistration] // 帶有自定義特性
public class MyService2 : IService2
{
    public void PerformService2()
    {
        Console.WriteLine("Service 2 performed.");
    }
}

// 實現類3,實現IService1和IService2
public class MyService3 : IService1, IService2
{
    public void PerformService1()
    {
        Console.WriteLine("Service 3 (Service 1 part) performed.");
    }

    public void PerformService2()
    {
        Console.WriteLine("Service 3 (Service 2 part) performed.");
    }
}

步驟2:創建自定義特性

// 自定義特性
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
sealed class CustomRegistrationAttribute : Attribute
{
}

步驟3:創建自動注冊方法

using System;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;

class Program
{
    static void Main()
    {
        // 創建服務集合
        var services = new ServiceCollection();

        // 步驟4:注冊指定類
        services.AddTransient<MyService1>();

        // 步驟5:注冊帶有自定義特性的類
        RegisterClassesWithAttribute<CustomRegistrationAttribute>(services);

        // 步驟6:注冊項目下所有帶有接口實現的類(項目下的所有接口)
        RegisterAllImplementationsOfInterfaces(services);

        // 構建服務提供程序
        var serviceProvider = services.BuildServiceProvider();

        // 步驟7:使用注冊的服務
        var myService1 = serviceProvider.GetService<MyService1>();
        myService1.PerformService1();

        var myService2 = serviceProvider.GetService<MyService2>();
        myService2.PerformService2();

        var myService3 = serviceProvider.GetService<MyService3>();
        myService3.PerformService1();
        myService3.PerformService2();
    }

    // 自動注冊帶有指定特性的類
    static void RegisterClassesWithAttribute<TAttribute>(IServiceCollection services)
        where TAttribute : Attribute
    {
        // 獲取當前程序集
        var assembly = Assembly.GetExecutingAssembly();

        // 獲取帶有指定特性的所有類
        var attributedTypes = assembly.GetTypes()
            .Where(type => type.GetCustomAttributes(typeof(TAttribute), true).Any() && type.IsClass);

        // 注冊這些類
        foreach (var attributedType in attributedTypes)
        {
            services.AddTransient(attributedType);
        }
    }

    // 自動注冊項目下所有帶有接口實現的類(項目下的所有接口)
    static void RegisterAllImplementationsOfInterfaces(IServiceCollection services)
    {
        // 獲取當前程序集
        var assembly = Assembly.GetExecutingAssembly();

        // 獲取項目下所有接口
        var interfaceTypes = assembly.GetTypes()
            .Where(type => type.IsInterface);

        // 獲取實現了這些接口的所有類
        var implementationTypes = assembly.GetTypes()
            .Where(type => interfaceTypes.Any(interfaceType => interfaceType.IsAssignableFrom(type)) && type.IsClass);

        // 注冊這些類
        foreach (var implementationType in implementationTypes)
        {
            services.AddTransient(implementationType);
        }
    }
}

在上述代碼中:

  • 使用AddTransient方法注冊了特定的MyService1類。
  • 使用RegisterClassesWithAttribute方法注冊了帶有CustomRegistrationAttribute特性的類。這里使用了反射機制來動態獲取所有帶有指定特性的類的類型,并將它們注冊到DI容器中。
  • 使用RegisterAllImplementationsOfInterfaces方法注冊了項目下所有實現接口的類。

請確保在項目中引用了Microsoft.Extensions.DependencyInjection相關的包。這是一個基本的示例,實際應用中可能需要更復雜的配置,具體取決于項目的需求。

責任編輯:姜華 來源: 今日頭條
相關推薦

2024-12-30 12:00:00

.NET Core依賴注入屬性注入

2024-11-27 00:24:04

2009-11-12 10:32:47

ADO.NET技術

2009-11-12 10:53:57

ADO.NET連接My

2010-01-14 13:59:01

2010-01-13 17:47:59

VB.NET拖放

2025-01-07 08:55:54

2024-06-12 00:00:01

Java函數式接口

2010-01-18 19:36:52

VB.NET調整控件

2010-01-14 10:07:08

VB.NET文件名排序

2023-06-28 08:16:50

Autofac應用程序

2010-10-22 11:31:53

SQL Server自

2009-11-24 15:34:41

DNS服務器組建

2024-08-26 08:27:18

2010-01-11 18:40:03

VB.NET操作注冊表

2009-02-16 15:35:00

2024-02-26 00:04:00

代碼zip()開發

2017-05-11 15:01:43

Androidweb布局

2009-12-11 15:37:58

Linux日志處理

2024-04-15 07:00:00

Python開發Hatch
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日本精品一区二区 | 久久久久久久亚洲精品 | 一区中文字幕 | 中文字幕一区在线观看视频 | 97久久久久久久久 | 免费观看一级特黄欧美大片 | 国产精品178页 | 久久久久国产精品午夜一区 | 日韩一区二区三区在线看 | 免费国产视频 | 天天爽综合网 | 久久久青草婷婷精品综合日韩 | 欧美二区在线 | 亚洲精品福利视频 | 久久www免费人成看片高清 | 欧美成年网站 | 国产一级片久久久 | 国产亚洲一区二区精品 | 亚洲精品二区 | 欧美成年网站 | 成人综合视频在线观看 | 日韩成人精品在线 | 久久精品99国产精品 | 久草在线在线精品观看 | 国产乱码精品一区二区三区忘忧草 | 欧日韩在线观看 | 成人在线一区二区 | 久久精品中文 | 久草色视频 | 日韩电影免费观看中文字幕 | www.久久久久久久久久久久 | 国产日韩欧美中文 | 国产九一精品 | 91在线播 | 九九热免费在线观看 | 美女黄色在线观看 | 日韩在线不卡 | 中文字幕精品一区 | 亚洲在线 | 91精品国产综合久久久久 | 久久丝袜视频 |