netcore 依据注解自动注入


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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM