码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET Core2,通过反射批量注入程序集

时间:2018-02-08 15:31:37      阅读:501      评论:0      收藏:0      [点我收藏+]

标签:key   div   asp   public   eric   foreach   int   class   程序集   

 public void ConfigureServices(IServiceCollection services)
        {
            string strValue = Configuration.GetSection("Appsetings").GetSection("key").Value;
            foreach (var item in GetClassName(strValue))
            {
                foreach (var typeArray in item.Value)
                {
                    services.AddScoped(typeArray, item.Key);
                }
            }
          
            services.AddMvc();
        }
        private static Dictionary<Type, Type[]> GetClassName(string assemblyName)
        {
            if (!String.IsNullOrEmpty(assemblyName))
            {
                Assembly assembly = Assembly.Load(assemblyName);
                List<Type> ts = assembly.GetTypes().ToList();

                var result = new Dictionary<Type, Type[]>();
                foreach (var item in ts.Where(s => !s.IsInterface))
                {
                    var interfaceType = item.GetInterfaces();
                    if (item.IsGenericType) continue;
                    result.Add(item, interfaceType);
                }
                return result;
            }
            return new Dictionary<Type, Type[]>();
        }

ASP.NET Core2,通过反射批量注入程序集

标签:key   div   asp   public   eric   foreach   int   class   程序集   

原文地址:https://www.cnblogs.com/Mrkaisa/p/8431355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!