Read From Config To Override Default Value With a Type Free Method On DotNet
Oct 29th 2008 | Posted by Kadir Pekel as .NET, ASP.NET, c#
Here is a little code snippet i have written recently for type free variable assignment from config’s appSettings node using c#
public static void ReadFromConfig<T>(string key, ref T obj)
{
string value = System.Configuration.ConfigurationManager.AppSettings[key];
if (!string.IsNullOrEmpty(value)) obj = (T)System.Convert.ChangeType(value, typeof(T));
}
The usage is simple as itself.
// initialize a variable with a default value
int foo = 9;
// change the value of foo if any value assigned to it from config
// otherwise do nothing
ReadFromConfig<int>("foosetting", ref foo);
Hope, this helps…
Subscribe in a reader
Subscribe via e-mail