Brandon Lewis

Brandon Lewis

  • NA
  • 527
  • 126k

Casting String To Generic Type

Feb 28 2008 8:42 PM
I have a method which I want to look like this:

public static T GetSetting<T>(SettingKeys key)
{
   return (T)settingList[key];
}

However, this will not compile of course but I hope you get the general idea :) I have a list of settings which need to be casted to certain data types based on what the setting is, and this generic method would be perfect if it would just work like this:

bool sendingEmails = GetSetting<bool>(SettingKeys.IsSendingEmail);

If it were only so simple! Do you guys know of any clean work around for this? Thanks!

Answers (1)