string str_type = "System.Int32"; Type type = Type.GetType(str_type); object object_NewValue = Activator.CreateInstance(type);
flash 里有 eval,但是c#里,好像不行
是不是在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());
要不你自己去定义一些class,或者定义一些类型
有一个猜想是否你想要枚举
我想有“abc”,"i"我想生成
sw.WriteLine("abc"+i.ToString())
但不知道如何把"i"变为i,和楼主的一个意思!同问!http://topic.csdn.net/u/20080414/14/e91498cf-8832-4a62-a06d-3ad210be903f.html
string str_type = "System.Int32";
Type type = Type.GetType(str_type);
object object_NewValue = Activator.CreateInstance(type);
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());
这个也可以吗?真不错,学习了一下,有空试一下。如果不可以,楼主大可以,这样做一些if else
try{
if( str_type == "int" )
你要转换的变量 = convert.toint32();//强行转换。
if( str_type == "string")
你要转换的变量 = convert.tostring();//强行转换。
....
else
//没有你输入的类型;
}
catch
{
//转换出错,数据有误
}
呵呵,方法笨,但就那么几种类型。也无所谓。
当然有那位兄弟说的方法更好了。
第二种:如果你也可以使用object转换
第三:重点是你要让别人知道你所做的是什么事情!
object object_NewValue = Activator.CreateInstance(str_type, true);
Type type = Type.GetType("int", false, true);
object object_NewValue = Activator.CreateInstance(type, true);
这种需求,一般都是前期没设计好导致的。如果架构合理的话根本不需要这样。
Type type = Type.GetType(str_type);
object object_NewValue = Activator.CreateInstance(type);
str_type object_NewValue; str_type 是一个变量,你不能用一个变量去作为另一个变量(object_NewValue)的类型定义;