string str_type=(string)"int"; 

解决方案 »

  1.   

    如果是.net framework 3.5, 并且是局部变量,可以用var关键字,由编译器动态决定其真实类型,但这与楼主要求也相差很多。个人认为在强类型的语言中,这种想法是不能实现的。关注楼下解答。
      

  2.   

    好像没有这种做法
    要不你自己去定义一些class,或者定义一些类型
    有一个猜想是否你想要枚举
      

  3.   

    明白楼主的意思的了,我最近有个地方也想这样,不知道如何实现
    我想有“abc”,"i"我想生成
    sw.WriteLine("abc"+i.ToString())
    但不知道如何把"i"变为i,和楼主的一个意思!同问!http://topic.csdn.net/u/20080414/14/e91498cf-8832-4a62-a06d-3ad210be903f.html
      

  4.   


    string str_type = "System.Int32";
    Type type = Type.GetType(str_type);
    object object_NewValue = Activator.CreateInstance(type);
      

  5.   

    flash 里有 eval,但是c#里,好像不行
      

  6.   

    是不是在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());
      

  7.   


    这个也可以吗?真不错,学习了一下,有空试一下。如果不可以,楼主大可以,这样做一些if else
    try{
    if( str_type == "int" )
        你要转换的变量 = convert.toint32();//强行转换。
    if( str_type == "string")
        你要转换的变量 = convert.tostring();//强行转换。
    ....
    else
        //没有你输入的类型;
    }
    catch
    {
    //转换出错,数据有误 
    }
    呵呵,方法笨,但就那么几种类型。也无所谓。
    当然有那位兄弟说的方法更好了。
      

  8.   

    第一种:简单的可以直接转换
    第二种:如果你也可以使用object转换
    第三:重点是你要让别人知道你所做的是什么事情!
      

  9.   

    Type str_type = Type.GetType("System.Int32", false, true);
    object object_NewValue = Activator.CreateInstance(str_type, true);
      

  10.   


    Type type = Type.GetType("int", false, true);
    object object_NewValue = Activator.CreateInstance(type, true);
    这种需求,一般都是前期没设计好导致的。如果架构合理的话根本不需要这样。
      

  11.   

    string str_type = "System.Int32";
    Type type = Type.GetType(str_type);
    object object_NewValue = Activator.CreateInstance(type);
      

  12.   

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

  13.   

    string str_type="int";
    str_type object_NewValue; str_type 是一个变量,你不能用一个变量去作为另一个变量(object_NewValue)的类型定义;