예제 #1
0
        private static void AddConfigs(IServiceCollection services)
        {
            services.AddSingleton(x => Cfg.ConfigFactory <GeneralConfig>());
            services.AddSingleton <ICfg, GeneralConfig>(x => x.GetService <GeneralConfig>());

            var contract = typeof(ICfg);

            var configTypes = AppDomain.CurrentDomain.GetAssemblies()
                              .SelectMany(x => x.GetTypes()
                                          .Where(y => contract.IsAssignableFrom(y) && !y.IsInterface && !y.IsAbstract && y != typeof(GeneralConfig)));

            foreach (var configType in configTypes)
            {
                services.AddSingleton(configType, x => Cfg.ConfigFactory(configType));
                services.AddSingleton(contract, x => x.GetService(configType));
            }
        }