you can use Convert.ChangeType
T GetValue<T>(string name)
{
string item = getstuff(name);
return (T)Convert.ChangeType(item, typeof(T));
}
if you need to restrict input types to int and DateTime only, add a condition like below
if (typeof(T) != typeof(int) && typeof(T) != typeof(DateTime))
{
}
source
share