首页 > Web开发 > 详细

netcore 依据注解自动注入

时间:2020-09-23 09:24:31      阅读:268      评论:0      收藏:0      [点我收藏+]
            DbCtxt.ConnStr = Configuration.GetSection("ConnectionString").Value;

            // 根据类是否添加注解[IsService]来自动注入
            var assm = new[] { typeof(Program).Assembly, typeof(DbCtxt).Assembly };
            foreach (var ass in assm)
            {
                var types = ass.GetTypes();
                foreach (var typ in types)
                {
                    var typeInfo = typ.GetTypeInfo();
                    if (typeInfo.IsDefined(typeof(IsService)))
                    {
                        services.AddScoped(typ);
                        var interfaces = typeInfo.ImplementedInterfaces;
                        foreach (var item in interfaces)
                        {
                            services.AddScoped(item, typ);
                        }
                    }
                }
            }

 

netcore 依据注解自动注入

原文:https://www.cnblogs.com/jonney-wang/p/13716086.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!