Created
February 24, 2014 14:39
-
-
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.
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
/// <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