Is there a way to make this method generic so I can return a string, bool, int, or double? Right now, it's returning a string, but if it's able find "true" or "false" as the configuration value, I'd like to return a bool for example.
public static string ConfigSetting(string settingName)
{
return ConfigurationManager.AppSettings[settingName];
}
Best Answer
You need to make it a generic method, like this:
But the caller will have to specify the type they expect. You could then potentially use
Convert.ChangeType
, assuming that all the relevant types are supported:I'm not entirely convinced that all this is a good idea, mind you...