1
Answer

Casting String To Generic Type

Brandon Lewis

Brandon Lewis

13y
5.3k
1
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)