C#中如何实现动态类型转换?(高手请进!)

tpwang 2003-12-18 10:49:55
小弟在想在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#里应该如实现?

请各位高手指教!


...全文
1564 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
allen1981813 2003-12-19
  • 打赏
  • 举报
回复
convert............
刘小浪 2003-12-19
  • 打赏
  • 举报
回复
马上写一个
public object getReturn(object obj)
{
if(obj==null || obj==System.DBNull.Value)
{
return 空; //可以 return null ? 我不知道能否返回(object)null.
}else
{
switch(obj.GetType().Name)
{
case "String":
return Convert.ToString(obj);
break;
.................
default:
try
{
return Convert.ToString(obj);
}catch
{
return 空;
}
}

}
}
majunjie74 2003-12-19
  • 打赏
  • 举报
回复
private static object TypeTranstion(string str,string type)
{
switch(type)
{
case "decimal":
return Decimal.Parse(str);
case "string":
return str;
case "int":
return (int)(Decimal.Parse(str));

}
return str;
}
cnhgj 2003-12-19
  • 打赏
  • 举报
回复
Convert.ToString(int);
Convert.ToInt32(string);
tpwang 2003-12-19
  • 打赏
  • 举报
回复
我先试试 xixigongzhu(夕夕公主) 的方法。

先谢谢各位!
qiujoe 2003-12-19
  • 打赏
  • 举报
回复
首先不清楚你是如何跟踪的?
如果你只是简单的调试,通过看返回值的数据类型得出你说的结论的话,我建议你试试xixigongzhu(夕夕公主) 的方法。你可以在调试状态下看看返回值的类型,也会是int, decimal..........
如果不是这样,请说明一下你的跟踪方法。
saucer 2003-12-19
  • 打赏
  • 举报
回复
you didn't get it, did you?

ADO.NET knows the types of your data, they are created in the right type, after all, .NET is strong-typed, but DataSet/DataTable/DataRow are generic structures, they have to contain any object, so the returned type has to be "object", although these objects have their own types
xixigongzhu 2003-12-19
  • 打赏
  • 举报
回复
这样就可以了:
public static Object 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);
.......
}
}
tpwang 2003-12-19
  • 打赏
  • 举报
回复
回复:qiujoe(迷糊)

我具体也跟踪过ado.net里而的DataColumn,DataRow的值,的确是根据数据库的对应字段生成
Boolean,Decimal,Int,String,.... 等类型的,并不是你所说的GetDouble,GetDecimal,才转
换的.

同时我希望大家能多提点建议.
tpwang 2003-12-19
  • 打赏
  • 举报
回复
我不是指它返回各种类型, 它里里生成若干对象,如每个表中若干字段,你编译时并不知道
这些对象的类型,只是连接数据源后才根据数据的类型生成的字段对象的类型.
我只想用这种方式,根据数据(不一定是数据库)定义的类型生成对象时在声明类型.

所以想请教各位高手有什么好提议?
qiujoe 2003-12-19
  • 打赏
  • 举报
回复
象ADO.NET里从数据库读出数据,然后每个字段的值都根据数据库的定义
定义好类型了,应用程序就直接可以用了,不用再进行类型转换了。

ADO.net好象不是你说的这样吧?
而是如思归所言
GetDouble(), GetDecimal(), GetString()...
saucer 2003-12-19
  • 打赏
  • 举报
回复
otherwise, you have to create a list of functions as in SqlDataReader, GetDouble(), GetDecimal(), GetString()...
saucer 2003-12-19
  • 打赏
  • 举报
回复
>>>ADO.NET本身也是这样做的,我只不过学它的做法吧了.

are you sure?? ADO.NET uses Object, if you want to return multiple types, use "Object", or wait for generics in .NET 1.2, it might help you out
tpwang 2003-12-19
  • 打赏
  • 举报
回复
回复:saucer(思归)
a function can only return one type or void
这个我知道,但可否有其它的方法?

如在方法里生成一个对象,它的类型在构建的时候生成的,程序里直接用这个对象的属性.
ADO.NET本身也是这样做的,我只不过学它的做法吧了.
bankliu 2003-12-19
  • 打赏
  • 举报
回复
~竟然会有如此提问者~
saucer 2003-12-19
  • 打赏
  • 举报
回复
a function can only return one type or void

>>>>我不想用ADO.NET,因为它里面定义了太多没用的东西了

照你这种逻辑,你也不该用.NET库,里面定义了太多的东西对你没用

自己写个语言,写个编译器。。。
tpwang 2003-12-19
  • 打赏
  • 举报
回复
多谢各位指点!结贴了 :)
brightheroes 2003-12-19
  • 打赏
  • 举报
回复
是不是ADO.NET里所有返回数型都是Object,都要自已强制转换成相应的类型的?
----〉

laserman 2003-12-19
  • 打赏
  • 举报
回复
DateTime dt = (DateTime)this.ds.Tables[0].Rows[0]["out_date"];

int i = (int)this.ds.Tables[0].Rows[0]["item_count"];

一定要转换
tpwang 2003-12-19
  • 打赏
  • 举报
回复
自已晕了 :(

是不是ADO.NET里所有返回数型都是Object,都要自已强制转换成相应的类型的?
加载更多回复(10)

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧