int i = 1;  //这里int可以为任意类型
object obj = i;
i = ? //利用反射怎么实现?

解决方案 »

  1.   

    我觉得这里不需要用到反射,而且在i为值类型或者为引用类型时,.NET的处理机制是不同的,请看下面这个简单的例子(解释在注释里):
            static void Main(string[] args)
            {
                Int32 i = 15;
                Object o1 = i; // box i to o1
                i = -15; // change the value of i
                Int32 i2 = (Int32)o1; // unbox o1 to i2
                Console.WriteLine("i: {0}; o1: {1}; i2: {2}", i, o1, i2);
                // Console output:
                // i: -15; o1: 15; i2: 15            StringBuilder s = new StringBuilder();
                s.Append("T1;");
                Object o2 = s; // reference assignment
                s.Append("T2");
                StringBuilder s2 = o2 as StringBuilder;
                Console.WriteLine("s: '{0}'; o2: '{1}'; s2: '{2}'", s, o2, s2);
                // Console output:
                // s: 'T1;T2'; o2: 'T1;T2'; s2: 'T1;T2'            Byte x = 16;
                Object ox = x;
                String sx = (String)ox; // System.InvalidCastException 
                Console.WriteLine("x: '{0}'; ox: '{1}'; sx: '{2}'", x, ox, sx);            Console.ReadLine();
            }
      

  2.   

    如果obj里存的是int
    则(int)obj
    如果obj里存的是数字的字符串如"123"
    则int.Parse(obj.ToString())
      

  3.   

    楼上全错啦,这里不是装箱拆箱问题,问题是,i不一定就是int,还可能是bool,double,string,ILiat<T>等任意类型,这里就是要获取obj.GetType(),然后根据其类型来转换。。
    就是把obj还原为真实的类型。还有人吗?
      

  4.   


    如果你的是成员变量,获取类型之后
    Type type=obj.GetType();
    FieldInfo fi=type.GetField("i",BindingFlags.Instance|BingdingFlags.Public|BidingFlags|NonPublic)
    fi.GetValue(obj,null)
    没打开开发工具,手写可能有错误,大致是这样。
      

  5.   

    用反射得到的也是object,需要用(类型)result转换为实际类型。
      

  6.   

    用.NET4.0里面的dynamic类型,自动在运行时转换为实际类型的。
      

  7.   

    如果仅知道object是数字,而不知确切类型(可能int,long,short,....),则需要判断类型int rst=0;
    do
    {
      if(obj is int)
      {
        rst =(int)obj; 
         break;
      }
      if(obj is short)
      {
         rst =(int)(short)obj;
         break;
      }
       ... 
    }while(false)
      

  8.   

    这种情况遇到过,感觉写的时候就像在做绕口令,绕来绕去绕不出那个怪圈,
    最后还是用
    object o = 1;
                if (o is int)
                {            }
                else if(o is string)
                { 
                
                }

    一个个比较解决问题(虽然这个方法并不是太好,但用用还是不错的)
      

  9.   

    除非一个个判断,并且还原了之后立刻用,不然还原了之后放哪呢?
    再放到个object里,那不又回去了么
      

  10.   

    “还原”的动机是什么?在还原时,你总要显式写上具体类型吧?那直接转有何不可?不管怎么样,可以从TypeConverter类来判断,也不知道是不是你想要的。
    class Program
        {
            static void Main(string[] args)
            {
                object obj1 = 123;
                bool check1 = checkConvert<int>(obj1);
                if (check1)
                {
                    Console.WriteLine("可以转为int");
                    int result = getReuslt<int>(obj1);
                    Console.WriteLine(string.Format("int的值为:{0}", result));
                }            object obj2 = "abc";
                bool check2 = checkConvert<int>(obj2);
                if (check2)
                {
                    Console.WriteLine("可以转为int");
                    int result = getReuslt<int>(obj2);
                    Console.WriteLine(string.Format("int的值为:{0}", result));
                }
                else
                {
                    Console.WriteLine("不能转为int");
                }
                bool check3 = checkConvert<string>(obj2);
                if (check3)
                {
                    Console.WriteLine("可以转为string");
                    string result = getReuslt<string>(obj2);
                    Console.WriteLine(string.Format("string的值为:{0}", result));
                }
                else
                {
                    Console.WriteLine("不能转为string");
                }            Console.ReadLine();
            }        private static bool checkConvert<T>(object obj)
            {
                TypeConverter converter = TypeDescriptor.GetConverter(obj);
                return converter.CanConvertTo(typeof(T));
            }        private static T getReuslt<T>(object obj)
            {
                TypeConverter converter = TypeDescriptor.GetConverter(obj);
                object result = converter.ConvertTo(obj, typeof(T));
                return (T)result;
            }
        }