Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuka1984/e96f8ac274b144a17d4d55145fed58af to your computer and use it in GitHub Desktop.
Save yuka1984/e96f8ac274b144a17d4d55145fed58af to your computer and use it in GitHub Desktop.
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