字符串"int"转换

jhtchina 2008-04-16 09:01:12
string str_type="int";
str_type object_NewValue;
这里"int"也可能是其他类型
怎么实现?
...全文
1396 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
schoollong 2008-04-16
  • 打赏
  • 举报
回复
另:楼主的本意好像自己没表达清楚

估计是想在代码运行时自动判别类型、声明变量

但借某楼上的话,还是要分清运行时和编译时。
gui0605 2008-04-16
  • 打赏
  • 举报
回复
没见过这样用法
schoollong 2008-04-16
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 hbxtlhx 的回复:]
C# code
string str_type = "System.Int32";
Type type = Type.GetType(str_type);
object object_NewValue = Activator.CreateInstance(type);
[/Quote]
这个可行,学习了

声明了object_NewValue之后,赋值时候却可以赋int sting bool等类型均不会报错,且不能调用相关类型的方法,感觉这个方法还是有点其他需要考虑的地方的


流浪河 2008-04-16
  • 打赏
  • 举报
回复
没理解你的意思 你想干嘛呢
chuxue1342 2008-04-16
  • 打赏
  • 举报
回复
这种的情况,用泛型比较合适!可以根据你的需要确定类型!
zengshunyou 2008-04-16
  • 打赏
  • 举报
回复
string str_type = "System.Int32";
Type type = Type.GetType(str_type);
object object_NewValue = Activator.CreateInstance(type);

睡神在睡觉 2008-04-16
  • 打赏
  • 举报
回复
强制类型转换,不可以么?LZ试试吧
wudichong 2008-04-16
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 shinaterry 的回复:]
C# codeType str_type = Type.GetType("System.Int32", false, true);
object object_NewValue = Activator.CreateInstance(str_type, true);
[/Quote]
HimeTale 2008-04-16
  • 打赏
  • 举报
回复

Type type = Type.GetType("int", false, true);
object object_NewValue = Activator.CreateInstance(type, true);


这种需求,一般都是前期没设计好导致的。如果架构合理的话根本不需要这样。
shinaterry 2008-04-16
  • 打赏
  • 举报
回复
Type str_type = Type.GetType("System.Int32", false, true);
object object_NewValue = Activator.CreateInstance(str_type, true);
KingNE 2008-04-16
  • 打赏
  • 举报
回复
没看明白
convert
enjoyeveryday 2008-04-16
  • 打赏
  • 举报
回复
第一种:简单的可以直接转换
第二种:如果你也可以使用object转换
第三:重点是你要让别人知道你所做的是什么事情!
abc83915 2008-04-16
  • 打赏
  • 举报
回复
没看明白。。不过先顶一个
zccmy22 2008-04-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ojekleen 的回复:]
Type t=Type.gettype("int");
[/Quote]

这个也可以吗?真不错,学习了一下,有空试一下。如果不可以,

楼主大可以,这样做一些if else
try{
if( str_type == "int" )
你要转换的变量 = convert.toint32();//强行转换。
if( str_type == "string")
你要转换的变量 = convert.tostring();//强行转换。
....
else
//没有你输入的类型;
}
catch
{
//转换出错,数据有误
}
呵呵,方法笨,但就那么几种类型。也无所谓。
当然有那位兄弟说的方法更好了。
viena 2008-04-16
  • 打赏
  • 举报
回复
关键字、类型是编译时的概念
字符串是运行时的概念
楼主先补补基本概念吧~
virusswb 2008-04-16
  • 打赏
  • 举报
回复
是不是在c++中的define,定义别名吧,c#中
str_type object_NewValue; 
好像不行
define type_int int;

看看下面这段可以实现你要的效果吗
string type_int="System.Int32";
Type type = Type.GetType(type_int,true,true);
object a = 0;
int b = (int)Activator.CreateInstance(type);
MessageBox.Show(b.ToString());
niyoler 2008-04-16
  • 打赏
  • 举报
回复
flash 里有 eval,但是c#里,好像不行
北京的雾霾天 2008-04-16
  • 打赏
  • 举报
回复

string str_type = "System.Int32";
Type type = Type.GetType(str_type);
object object_NewValue = Activator.CreateInstance(type);
cch1010 2008-04-16
  • 打赏
  • 举报
回复
学习哈
cc_net 2008-04-16
  • 打赏
  • 举报
回复
明白楼主的意思的了,我最近有个地方也想这样,不知道如何实现
我想有“abc”,"i"

我想生成


sw.WriteLine("abc"+i.ToString())


但不知道如何把"i"变为i,和楼主的一个意思!同问!

http://topic.csdn.net/u/20080414/14/e91498cf-8832-4a62-a06d-3ad210be903f.html
加载更多回复(15)

110,538

社区成员

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

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

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