Last active
September 19, 2018 16:42
-
-
Save adams85/662ac5d2a03f236dc73f00bbfd9aebf8 to your computer and use it in GitHub Desktop.
Sample code for https://stackoverflow.com/questions/52407867/how-to-initialize-a-dictionary-type-during-dependency-injection/52409026
This file contains hidden or 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
{ | |
"PhoneBookSection": { | |
"John Doe": "555-123-456", | |
"Jane Doe": "555-234-567" | |
} | |
} |
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using Microsoft.Extensions.Configuration; | |
namespace Sample | |
{ | |
class Program | |
{ | |
static readonly IConfiguration Configuration = new ConfigurationBuilder() | |
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false) | |
.Build(); | |
static void Main(string[] args) | |
{ | |
const string PhoneBookSection = "PhoneBookSection"; | |
var phonebook = new Dictionary<string, string>(); | |
Configuration.Bind(PhoneBookSection, phonebook); | |
foreach (var entry in phonebook) | |
Console.WriteLine($"{entry.Key} - {entry.Value}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment