C# , .netWebApi, WPF 用特性实现类似Java 的Ioc 自动装配@Autowired

写C# 一直很羡慕Java的@Autowired 自动装配. 因为C# 必须手动在Ioc里注册

之前用接口实现了自动注册IOC, 总是觉得美中不足, 毕竟没有真正实现用注解/特性实现自动注入, 这次我们来实现一个用特性注入Ioc的扩展方法.

csharp 复制代码
namespace MyCode.BLL.Service.Ioc
{
    /// <summary>
    /// 类型的生命周期枚举
    /// </summary>
    public enum Lifetime
    {
        /// <summary>
        /// 单例
        /// </summary>
        Singleton,
        /// <summary>
        /// 多例
        /// </summary>
        Transient,
        Scoped

    }

    /// <summary>
    /// 标注类型的生命周期、是否自动初始化
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class ExposedServiceAttribute : Attribute
    {
        public Lifetime Lifetime { get; set; }

        public bool AutoInitialize { get; set; }

        public Type[] Types { get; set; }

        public ExposedServiceAttribute(Lifetime lifetime = Lifetime.Transient, params Type[] types)
        {
            Lifetime = lifetime;
            Types = types;
        }
    }
}
csharp 复制代码
using Microsoft.Extensions.DependencyInjection;
using MyCode.BLL.Service.Ioc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace MyCode.Utils.AttributeIoc
{
    public static class DependencyExtension
    {
        /// <summary>
        /// 获取class, 非抽象类, 特性有ExposedServiceAttribute 注解
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        //private static List<Type> GetTypes(Assembly assembly)
        //{
        //    var result = assembly
        //        .GetTypes()
        //        .Where(
        //            t =>
        //                t != null
        //                && t.IsClass
        //                && !t.IsAbstract
        //                && t.CustomAttributes.Any(
        //                    p => p.AttributeType == typeof(ExposedServiceAttribute)
        //                )
        //        )
        //        .ToList();

        //    return result;
        //}

        private static List<Type> MyGetTypes()
        {
            //获取当前程序集
            var entryAssembly = Assembly.GetEntryAssembly();
            var types = entryAssembly!
                .GetReferencedAssemblies() //获取当前程序集所引用的外部程序集
                .Select(Assembly.Load) //装载
                .Concat(new List<Assembly>() { entryAssembly }) //与本程序集合并
                .SelectMany(x => x.GetTypes()) //获取所有类
                .Where(
                    t =>
                        t != null
                        && t.IsClass
                        && !t.IsAbstract
                        && t.CustomAttributes.Any(
                            p => p.AttributeType == typeof(ExposedServiceAttribute)
                        )
                )
                .Distinct() //排重
                .ToList();
            ; 

            return types;
        }

        //public static void RegisterAssembly(this IServiceCollection services, Assembly assembly)
        //{
        //    var list = GetTypes(assembly);
        //    foreach (var type in list)
        //    {
        //        RegisterAssembly(services, type);
        //    }
        //}

        /// <summary>
        /// 加this 表示 IServiceCollection 的扩展方法
        /// </summary>
        /// <param name="services"></param>
        public static void RegisterAssembly(this IServiceCollection services)
        {
            var list = MyGetTypes();
            foreach (var type in list)
            {
                RegisterAssembly(services, type);
            }
        }



        public static void RegisterAssembly(IServiceCollection services, Type type)
        {
            var list = GetExposedServices(type).ToList();

            foreach (var item in list)
            {
                switch (item.Lifetime)
                {
                    case Lifetime.Singleton:
                        services.AddSingleton(type);
                        break;
                    case Lifetime.Transient:
                        services.AddTransient(type);
                        break;
                    case Lifetime.Scoped:
                        services.AddScoped(type);
                        break;
                    default:
                        break;
                }

                foreach (var IType in item.Types)
                {
                    switch (item.Lifetime)
                    {
                        case Lifetime.Singleton:
                            services.AddSingleton(IType, type);
                            break;
                        case Lifetime.Transient:
                            services.AddTransient(IType, type);
                            break;
                        case Lifetime.Scoped:
                            services.AddScoped(IType, type);
                            break;
                        default:
                            break;
                    }
                }
            }
        }

在Ioc注册:

csharp 复制代码
services.RegisterAssembly();

在View中使用:

csharp 复制代码
using MyCode.BLL.Service.Ioc;
using System.Windows;

namespace MyCode.Views
{
    /// <summary>
    /// MainView.xaml 的交互逻辑
    /// </summary>

    [ExposedService(Lifetime.Singleton)]
    public partial class MainView : Window
    {
        public MainView()
        {
            InitializeComponent();
        }
    }
}

成功结果:

相关推荐
xiaoshuaishuai85 小时前
C# GPU算力与管理
开发语言·windows·c#
hez20108 小时前
C# 15 类型系统改进:Union Types
c#·.net·.net core
FL16238631298 小时前
基于C#winform部署软前景分割DAViD算法的onnx模型实现前景分割
开发语言·算法·c#
C#程序员一枚9 小时前
高可用(High Availability, HA)
数据库·c#
weixin_5206498710 小时前
C#进阶-特性全知识点总结
开发语言·c#
fengyehongWorld11 小时前
C# 创建vba用的类库
c#
澄澈青空~11 小时前
有一个叫R2C,也有一个叫G2C
java·数据库·人工智能·c#
PGFA13 小时前
深度剖析 C# LINQ 底层执行机制:别让你的应用内存莫名其妙“爆”掉!
c#·solr·linq
2601_9498146915 小时前
如何使用C#与SQL Server数据库进行交互
数据库·c#·交互
CSharp精选营16 小时前
C#事务处理最佳实践:别再让“主表存了、明细丢了”的破事发生
c#·try-catch·事务处理·transactionscope