Created
February 28, 2019 11:45
-
-
Save csharpforevermore/b7f2ed8e97d0eb8a2c5ebc62d802d236 to your computer and use it in GitHub Desktop.
Nicer strongly-typed way to get a value from a web.config or app.config file. Sourced from http://byatool.com/uncategorized/configurationmanager-and-appsettings-a-wrapper-class-story/
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
protected static T? GetValueFromConfiguration<T>(String key) where T : struct | |
{ | |
T? returnValue = null; | |
if (System.Configuration.ConfigurationManager.AppSettings[key] != null) | |
{ | |
returnValue = System.Configuration.ConfigurationManager.AppSettings[key] | |
.ConvertTo<T>(); //MY AWSOME CONVERTTO METHOD!!11 | |
} | |
return returnValue; | |
} |
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
public static DateTime? TestDate | |
{ | |
get | |
{ | |
return GetValueFromConfiguration<DateTime>("TestDate"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment