Skip to content

Instantly share code, notes, and snippets.

@csharpforevermore
Created February 24, 2014 14:39
Show Gist options
  • Save csharpforevermore/9189507 to your computer and use it in GitHub Desktop.
Save csharpforevermore/9189507 to your computer and use it in GitHub Desktop.
This static helper class allows the easy generation of a strongly typed web.config value.
/// <summary>
/// Helper class for accesing configuration file
/// </summary>
public class ConfigurationHelper
{
/// <summary>
/// Returns strongly typed application setting value
/// </summary>
/// <typeparam name="T">Entry type</typeparam>
/// <param name="key">configuraiton key</param>
/// <param name="defaultValue">default value that should be returned if conversion fails</param>
/// <returns>configuration setting value</returns>
public static T GetAppSetting<T>(string key, T defaultValue)
{
var reader = new AppSettingsReader();
try
{
return (T)reader.GetValue(key, typeof(T));
}
catch (InvalidOperationException)
{
return defaultValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment