将字符串转换为T [英] Convert string to T
本文介绍了将字符串转换为T的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个方法需要将字符串转换为通用类型:
I have a method that needs to convert a string to the generic type:
T GetValue<T>(string name)
{
string item = getstuff(name);
return item converted to T // ????????
}
T可以是int或日期.
T could be int or date.
推荐答案
,您可以使用如果您只需要限制int和DateTime的输入类型,请添加以下条件
if you need to limit input types only for int and DateTime, add condition like below
if (typeof(T) != typeof(int) && typeof(T) != typeof(DateTime))
{
// do something with other types
}
这篇关于将字符串转换为T的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文