.NET CORE 属性IOC注入

本文介绍了如何使用自定义DIServicesAttribute注解标记服务,并通过DIServiceExtensions扩展类注册所有应用域中的此类服务,支持不同生命周期和接口服务类型的选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    //使用:services.AddDIServices();

    /// <summary>
    /// 标记服务  
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
    public class DIServicesAttribute : Attribute
    {
        /// <summary>
        /// 生命周期
        /// </summary>
        public ServiceLifetime Lifetime { get; set; } = ServiceLifetime.Singleton;

        /// <summary>
        /// 指定服务类型
        /// </summary>
        public Type ServiceType { get; set; }

        /// <summary>
        /// 是否可以从第一个接口获取服务类型
        /// </summary>
        public bool InterfaceServiceType { get; set; } = true;

        /// <summary>
        /// 服务(实现)唯一标识
        /// </summary>
        public string Identifier { get; set; }

        public DIServicesAttribute()
        {
        }

        public DIServicesAttribute(Type serviceType) : this(serviceType, ServiceLifetime.Singleton, null, false)
        {
        }

        public DIServicesAttribute(ServiceLifetime serviceLifetime) : this(null, serviceLifetime, null, true)
        {
        }

        public DIServicesAttribute(string identifier) : this(null, ServiceLifetime.Singleton, identifier, true)
        {
        }

        private DIServicesAttribute(Type serviceType, ServiceLifetime serviceLifetime, string identifier, bool interfaceServiceType)
        {
            ServiceType = serviceType;
            Lifetime = serviceLifetime;
            Identifier = identifier;
            InterfaceServiceType = interfaceServiceType;
        }

    }


    public static class DIServiceExtensions
    {
        /// <summary>
        /// 注册应用程序域中所有有DIServices特性的类
        /// </summary>
        /// <param name="services"></param>
        public static void AddDIServices(this IServiceCollection services)
        {
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (var type in assembly.GetTypes())
                {
                    foreach (var serviceAttribute in type.GetCustomAttributes<DIServicesAttribute>())
                    {
                        if (serviceAttribute != null)
                        {
                            var serviceType = serviceAttribute.ServiceType;

                            //类型检查,如果 type 不是 serviceType 的实现或子类或本身
                            //运行时 type 将无法解析为 serviceType 的实例
                            if (serviceType != null && !serviceType.IsAssignableFrom(type))
                            {
                                serviceType = null;
                            }

                            if (serviceType == null && serviceAttribute.InterfaceServiceType)
                            {
                                //serviceType = type.GetInterfaces().FirstOrDefault();
                                serviceType = type.GetInterfaces().LastOrDefault();
                            }

                            if (serviceType == null)
                            {
                                serviceType = type;
                            }

                            switch (serviceAttribute.Lifetime)
                            {
                                case ServiceLifetime.Singleton:
                                    services.AddSingleton(serviceType, type);
                                    break;
                                case ServiceLifetime.Scoped:
                                    services.AddScoped(serviceType, type);
                                    break;
                                case ServiceLifetime.Transient:
                                    services.AddTransient(serviceType, type);
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                }
            }
        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

letisgoto

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值