bool b = TypeDescriptor.GetConverter(typeof(int)).CanConvertTo(typeof(string));
// true
bool b2 = TypeDescriptor.GetConverter(typeof(int)).CanConvertFrom(typeof(string));
// true实在不明白 上面的的代码运行后返回的值都是 true ? 看了msdn的描述 b2 应该是 false 才对啊,有用过这个类的朋友帮忙解释一下,谢谢
// true
bool b2 = TypeDescriptor.GetConverter(typeof(int)).CanConvertFrom(typeof(string));
// true实在不明白 上面的的代码运行后返回的值都是 true ? 看了msdn的描述 b2 应该是 false 才对啊,有用过这个类的朋友帮忙解释一下,谢谢
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
我们也可以设计一个自己的类,使得它同样知道如何转换为字符串和从字符串转换回来。但是,要求string能够转换为任何类则要求过高了,因为string太普通了。
所以当你拿到的是string的TypeConverter的时候,返回不支持同样非常正常。