怎么实现函数ChangeType,能达到如下功能? 
 
int a= ChangeType( Object,a.GetType());
string b= ChangeType( Object,b.GetType());
bool c= ChangeType( Object,c.GetType());
 
就是根据前面的类型自动实现转换Object为该强类型?
 
支持常规的int,String,bool等类型就可以.
 
.Net2.0实现,和.Net3.0以上实现.注意:
Convert.ChangeType实现不了这个功能.
因为他还需要强制转换,如:int i = (int)Convert.ChangeType(d, typeof(int)); 

解决方案 »

  1.   

    这题不难吧.......既然你是规定可以用函数实现...既然你规定了2个传入参数那结果不就出来了么,过程不过是些判断。比如 "s"强行转换成 int的话 是直接抛出异常 还是给一个ascii值。 等等。
      

  2.   

    void ChangeType<T>(Object o, typeof(T), out T result)
    {
        result = (T)Convert.ChangeType(o, typeof(T));
    }int a;
    ChangeType<int>(o, a.GetType(), out a);
      

  3.   

    这个是不可能,你若是希望ChangeType有时的返回类型是int,有时是string,这岂不是开玩笑。除非定义一个泛型版本的ChangeType:public T ChangeType<T>(object obj)
    {
         return T(obj);
    }
      

  4.   

    你的意思是传一个值,自动转换类型,是数字就INT  是bit就bool 是文字就STRING 撒这不就是一个判断的问题了么了,如果你BIT 传的是 true 这种 不是 0或1  那就好办,直接判断啊  是否是bool 如果是不 的话就 强制转换INT  啊  有异常就说明只是string类型了啊。
      

  5.   

    class Program
        {
            static void Main(string[] args)
            {
                Object ob = 3;
                int a = ChangeType<int>(ob);
                Console.WriteLine(a.GetType());
                Console.WriteLine(a);
                Console.WriteLine(ChangeType<string>(ob).GetType());
            }
            static T ChangeType<T>(object oj)
            {
                T result = (T)Convert.ChangeType(oj, typeof(T));
                return result;
            }    }
      

  6.   

    LZ,你所说的转换都不是问题。有点障眼法的意思,我开始以为你只是需要转换。
    如果只需要转换的话把void函数就行,在里面把值赋给全局变量 到时候取就可以你不如这样问:怎么让一个函数既能返回int值又能返回string值,还能反回bool值
    是这样吧?
      

  7.   


      开始我也这么想的, 但是这个 不符合他的要求ChangeType<int>(ob);
      

  8.   

    public interface TypeConverter
    {
     public Object convertValue(Map context, Object target, Member member, String propertyName,   Object  value, Class toType);
    }
    看ognl源代码,不知道NET有没有对应版本。改改即可!
      

  9.   

    获得3个MSVIP称号,怎么可能是对C#泛型不了解的人呢。
      

  10.   

    代码的内部很好工作,假设传入的参数是 obj,deltailTypeswitch(detailType)
    {
        case "System.Int32":
            return (int)Convert.ChangeType(obj,int);
        case "System.Boolean":
            return (bool)Convert.ChangeType(obj,bool);
        default:
            return obj.ToString();
    }但是,你的函数签名怎么写?写成int ChangeType(object obj,Type detailType),返回不了bool类型
    写成bool ChangeType(object obj,Type detailType),返回不了int类型
    两个都写上去?编译器不认写成object ChangeType(object obj,Type detailType),你返回的是object,仍然要再去转换成int或者bool楼主,你这个问题我之前也想过,但是最后发现这个在目前无法实现
      

  11.   

    原因就是因为.NET是强类型语言
    如javascript之类的弱类型语言,应该能实现
      

  12.   

    不知道这样可不可以
            static object ChangeType(object o,Type t)
            {
                if (t.Name == "Int32")
                    return (int)o;
                else
                {
                    return (string)o;
                }
            }
    调用时
                var a=ChangeType(1,typeof(int));
                Console.WriteLine(a.GetType());
                Console.ReadKey();
      

  13.   

    怎么实现函数ChangeType,能达到如下功能? int a= ChangeType( Object,a.GetType());
    string b= ChangeType( Object,b.GetType());
    bool c= ChangeType( Object,c.GetType());就是根据前面的类型自动实现转换Object为该强类型?支持常规的int,String,bool等类型就可以..Net2.0实现,和.Net3.0以上实现.注意:
    Convert.ChangeType实现不了这个功能.
    因为他还需要强制转换,如:int i = (int)Convert.ChangeType(d, typeof(int));
    class Program
        {
            static void Main(string[] args)
            {
                Object ob = 3;
                int a = ChangeType<int>(ob);
                Console.WriteLine(a.GetType());
                Console.WriteLine(a);
                Console.WriteLine(ChangeType<string>(ob).GetType());
            }
            static T ChangeType<T>(object oj)
            {
                T result = (T)Convert.ChangeType(oj, typeof(T));
                return result;
            }    }这个好像是对的 学习了
      

  14.   

            public static int ChangeType (object obj,int input)  
            {
                return (int)obj ;
            }
            public static string ChangeType(object obj, string input)
            {         
                    return (string)obj;
            }
            public static bool ChangeType(object obj, bool input)
            {
                return (bool)obj;
            }
            public static DateTime ChangeType(object obj, DateTime input)
            {
                return (DateTime)obj;
            }
      

  15.   

    private static void Main(string[] args)
    {
        object obj;    obj = 123;
        int a = ChangeType(obj, typeof(int));
        Console.WriteLine(a);    obj = "abc";
        string b = ChangeType(obj, typeof(string));
        Console.WriteLine(b);    obj = true;
        bool c = ChangeType(obj, typeof(bool));
        Console.WriteLine(c);
    }static MixedType ChangeType(object value, Type type)
    {
        return new MixedType(value, type);
    }struct MixedType
    {
        private object _value;
        private Type _type;    public MixedType(object value, Type type)
        {
            _value = value;
            _type = type;
        }    public static implicit operator int(MixedType mixedType)
        {
            return (int)Convert.ChangeType(mixedType._value, mixedType._type); ;
        }    public static implicit operator bool(MixedType mixedType)
        {
            return (bool)Convert.ChangeType(mixedType._value, mixedType._type); ;
        }    public static implicit operator string(MixedType mixedType)
        {
            return (string)Convert.ChangeType(mixedType._value, mixedType._type); ;
        }
    }
      

  16.   

    恩 .net实现 泛型就可以
      

  17.   

    如果仅仅是换类型倒是可以这么做:        public static object ChangeType(object o, Type t)
            {
                return Activator.CreateInstance(t);
            }不知道你转换的逻辑打算怎么做。
      

  18.   


    我看了题目,以为  ChangeType 方法的第二个参数 是一个 System.Type类型,如果是那样的话
    在C#中,只好用泛型
    在IL里面,倒是可以,IL倒是能够依照不同的返回值来识别重载函数
    没想到.. ... 这有点欺负人了啊~~~
      

  19.   

    你下面这句没有说明白,有歧义:
    就是根据前面的类型自动实现转换Object为该强类型如果你是想用泛型转换,如楼上那几位所说,那么你这个题目也太简单了,谈不上“考考你”。我不太明白你的意思,如果用泛型,那这题也太简单了吧。T changeType<T>(object o)
    {
    T result = (T)Convert.ChangeType(oj, typeof(T));
    return result;}
      

  20.   

    你这也算答案?方法重载?你这题目不是脑筋急转弯吧。
    如果你还需要转换成其他100种类型呢?你写100个方法重载,如果1万个类型呢?
    public static int ChangeType (object obj,int input)
    {
    return (int)obj ;
    }
    public static string ChangeType(object obj, string input)
    {
    return (string)obj;
    }
    public static bool ChangeType(object obj, bool input)
    {
    return (bool)obj;
    }
    public static DateTime ChangeType(object obj, DateTime input)
    {
    return (DateTime)obj;
    }
      

  21.   

    int a= ChangeType( Object,a.GetType());
    参数可以是类名??
      

  22.   

    算你int a= ChangeType( Object,a.GetType());中的Object 是 obj,
    可a.GetType() 的类型是 Type,你的答案第二个参数的类型根本不是Type