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();
        }
    }
}

成功结果:

相关推荐
懒人咖4 分钟前
缺料分析时携带用料清单的二开字段
c#·金蝶云星空
bugcome_com42 分钟前
深入了解 C# 编程环境及其开发工具
c#
wfserial2 小时前
c#使用微软自带speech选择男声仍然是女声的一种原因
microsoft·c#·speech
阔皮大师4 小时前
INote轻量文本编辑器
java·javascript·python·c#
聆风吟º4 小时前
CANN hccl 深度解析:异构计算集群通信库的跨节点通信与资源管控实现逻辑
人工智能·wpf·transformer·cann
kylezhao20195 小时前
C# 中的 SOLID 五大设计原则
开发语言·c#
啦啦啦_99995 小时前
Redis-5-doFormatAsync()方法
数据库·redis·c#
Porco.w6 小时前
C#与三菱PLC FX5U通信
网络·c#
E_ICEBLUE8 小时前
PPT 批量转图片:在 Web 预览中实现翻页效果(C#/VB.NET)
c#·powerpoint·svg
JQLvopkk10 小时前
C# 轻量级工业温湿度监控系统(含数据库与源码)
开发语言·数据库·c#