Created
November 5, 2016 06:56
-
-
Save yuka1984/e96f8ac274b144a17d4d55145fed58af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class LoadConfigurationServiceCollectionExtensions | |
{ | |
public static IServiceCollection LoadConfiguration(this IServiceCollection services, IConfigurationSection containerSection) | |
{ | |
OptionsConfigurationServiceCollectionExtensions | |
.Configure<RegisterSetting>(services, containerSection) | |
.BuildServiceProvider().GetService<IOptions<RegisterSetting>>().Value | |
.Registers.ForEach(reg => | |
{ | |
var regassembly = string.IsNullOrWhiteSpace(reg.TypeAssembly) | |
? Assembly.GetEntryAssembly() : Assembly.Load(new AssemblyName(reg.TypeAssembly)); | |
var registType = regassembly.GetType(reg.Type); | |
if (registType == null) return; | |
var typeassembly = string.IsNullOrWhiteSpace(reg.MapToAssembly) | |
? Assembly.GetEntryAssembly() : Assembly.Load(new AssemblyName(reg.MapToAssembly)); | |
var maptoType = typeassembly.GetType(reg.MapTo); | |
if (maptoType == null) return; | |
services.Add(new ServiceDescriptor(registType, maptoType, reg.Lifetime)); | |
}); | |
return services; | |
} | |
private class RegisterSetting | |
{ | |
public class Register | |
{ | |
public string Type { get; set; } | |
public string TypeAssembly { get; set; } | |
public string MapTo { get; set; } | |
public string MapToAssembly { get; set; } | |
public ServiceLifetime Lifetime { get; set; } | |
} | |
public List<Register> Registers { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment