Skip to content

Instantly share code, notes, and snippets.

@csharpforevermore
Created February 28, 2019 11:45
Show Gist options
  • Save csharpforevermore/b7f2ed8e97d0eb8a2c5ebc62d802d236 to your computer and use it in GitHub Desktop.
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/
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;
}
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