【发布时间】:2015-09-01 18:24:40
【问题描述】:
为什么那些返回true:
TypeDescriptor.GetConverter(typeof(double)).CanConvertTo(typeof(double));
TypeDescriptor.GetConverter(typeof(int)).CanConvertTo(typeof(int));
什么时候返回false?
TypeDescriptor.GetConverter(typeof(decimal)).CanConvertTo(typeof(decimal));
TypeDescriptor.GetConverter(typeof(bool)).CanConvertTo(typeof(bool));
考虑到 GetConverter 返回的所有转换器都应该只将类型转换为字符串和从字符串转换的事件:
我正在使用 .NET Framework 4.5.2。
【问题讨论】:
-
我不确定除了 BCL 团队的成员之外,是否有任何人能够真正解释这是否有实际原因,或者这是否只是一个疏忽。我确实在BaseNumberConverter.CanConvertTo 中看到了检查原始类型的逻辑,其中 Decimal 是 not,从而解释了与 Decimal 的差异。但是为什么不支持 Decimal 呢?
标签: c# type-conversion