Skip to content

Instantly share code, notes, and snippets.

@arnabdas
Created April 4, 2020 19:27
Show Gist options
  • Save arnabdas/5bfec1419d66aca4217e182e6c10313a to your computer and use it in GitHub Desktop.
Save arnabdas/5bfec1419d66aca4217e182e6c10313a to your computer and use it in GitHub Desktop.
Using Appsettings In Console Applications - Taken from Seeman
{
"messageWriter":
"Ploeh.Samples.HelloDI.Console.ConsoleMessageWriter, HelloDI.Console"
}
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
string typeName = configuration["messageWriter"];
Type type = Type.GetType(typeName, throwOnError: true);
IMessageWriter writer = (IMessageWriter)Activator.CreateInstance(type);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment