【发布时间】:2012-02-24 16:08:32
【问题描述】:
你有这些代码的替代品吗,我想要一个更通用的代码 我尝试了转换类,但没有成功
public object convert(Type type, string value)
{
object r = null;
if (type == typeof(bool))
{
r = bool.Parse(value);
}
else if (type == typeof(int))
{
r = int.Parse(value);
}
else if (type == typeof(string))
{
r = value;
}
return r;
}
【问题讨论】:
-
可能会有很大的变化,但你可以(同时提出其他问题)通过使用动态来摆脱整个问题。
标签: c# type-conversion