如果我有一个可以是int,double或string的变量“param”,如何以最有效的方式将另一个字符串赋值给param?目前我在做什么是这样的:将字符串转换为另一个变量的类型
string s = "5";
switch (param)
{
case param.GetType() == "System.Double":
param = Convert.ToDouble(s);
break;
case param.GetType() == "System.Int32":
param = Convert.ToInt32(s);
break;
case param.GetType() == "System.String":
default:
break;
}
我希望能浓缩成这样的(伪代码):
param = (typeof(param))s;
或
param = s as tyepof(param);
而不是依靠[Magic Strings](https://en.wikipedia.org/wiki/Magic_number_(编程)),我强烈建议您使用[TypeCode Enumeration](https://msdn.microsoft .COM/EN-US /库/ system.typecode(v = vs.110)的.aspx)。 –
请尝试从此https://msdn.microsoft.com/en-us/library/bb384043.aspx –