C#中如何实现动态类型转换?(高手请进!)
小弟在想在C#中实现如下方法:
调用 value = TypeTranstion("1234.5678","decimal")
返回 decimal类型的 1234.5678
调用 value = TypeTranstion("1234.5678","string")
返回 string类型的 "1234.5678"
调用 value = TypeTranstion("1234.5678","decimal")
返回 int类型的 1234
建一个方法可能如下,但该方法不可能在C#里编译通进的,
因为C#里好象要先定义方法的返回类型的.
class TypeTranstion()
{
public TypeTranstion(string str,string type)
switch(type)
{
case (type =="decimal")
return Decimal.Parse(str);
case (type =="string")
return str;
case (type =="int")
return Int32.Parse(str);
.......
}
}
请问要实现以上的动态类型转换,在C#里应该如实现?
请各位高手指教!