bool b = TypeDescriptor.GetConverter(typeof(int)).CanConvertTo(typeof(string));
            // true
            bool b2 = TypeDescriptor.GetConverter(typeof(int)).CanConvertFrom(typeof(string));
            // true实在不明白 上面的的代码运行后返回的值都是 true ? 看了msdn的描述 b2 应该是 false 才对啊,有用过这个类的朋友帮忙解释一下,谢谢 

解决方案 »

  1.   

    既然支持整数转换为字符串(int converto string),为什么不支持字符串转换为整数(int convertfrom string)呢?你说的“msdn的描述”的链接是什么?
      

  2.   

    回复1楼CanConvertTo 方法  http://msdn.microsoft.com/zh-cn/library/system.componentmodel.typeconverter.canconvertto.aspx
    CanConvertFrom 方法  http://msdn.microsoft.com/zh-cn/library/system.componentmodel.typeconverter.canconvertfrom.aspx可是
    bool b = TypeDescriptor.GetConverter(typeof(string)).CanConvertTo(typeof(int));
    // false
    bool b2 = TypeDescriptor.GetConverter(typeof(string)).CanConvertFrom(typeof(int));
    // false上面的代码运行的结果都是false
      

  3.   

    当你拿到的是int的TypeConverter,作为整数,知道如何以字符串来表示自己,以及如何从输入的字符串来转换为整数非常正常。
    我们也可以设计一个自己的类,使得它同样知道如何转换为字符串和从字符串转换回来。但是,要求string能够转换为任何类则要求过高了,因为string太普通了。
    所以当你拿到的是string的TypeConverter的时候,返回不支持同样非常正常。