Mat*_*son 5 c# type-conversion
似乎TypeConverter.IsValid()使用当前的线程文化,但TypeConverter.ConvertFrom()没有。
这使得TypeConverter.IsValid()与DateTime类型一起使用非常无用,除非您处于不变文化中。确实,这似乎是一个错误。
有谁知道如何TypeConverter.IsValid()利用当前的文化?
下面的代码演示了这个问题。
它使用两个字符串,一个是 DD/MM/YYYY 格式,一个是 MM/DD/YYYY 格式。
测试的第一部分是在不变文化中完成的。它表明 TypeConverter.IsValid()对于 MM/DD/YYYY 字符串返回 true 并且您可以使用TypeConverter.ConvertFrom()该字符串将该字符串转换为DateTime.
第一部分还演示了TypeConverter.IsValid()为 DD/MM/YYYY 字符串返回 false。
对于第二部分,我更改了使用“DD/MM/YYYY”日期的当前文化“en-GB”。
我现在希望IsValid()结果被反转,但它返回的两个字符串与以前相同。所以它说 MM/DD/YYYY 字符串是有效的。
但是 - 这是重要的一点 - 如果您尝试使用TypeConverter.ConvertFrom()实际转换表示没问题的字符串TypeConverter.IsValid(),您将收到异常。
有没有办法解决这个问题?
using System;
using System.ComponentModel;
using System.Globalization;
using System.Threading;
namespace Demo
{
class Program
{
void Run()
{
// Start off with the US culture, which has MM/DD/YYYY date format.
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var dateConverter = TypeDescriptor.GetConverter(typeof(DateTime));
var goodDateString = "07/19/1961";
var badDateString = "19/07/1961";
test(dateConverter, goodDateString, "DateTime"); // Says it's good.
test(dateConverter, badDateString, "DateTime"); // Says it's bad.
var dateTimeValue = (DateTime) dateConverter.ConvertFrom(goodDateString);
Console.WriteLine("dateTimeValue = " + dateTimeValue);
Console.WriteLine();
// Now lets change the current culture to the UK, which has DD/MM/YYYY date format.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
dateConverter = TypeDescriptor.GetConverter(typeof(DateTime));
test(dateConverter, goodDateString, "DateTime"); // Still says it's good.
test(dateConverter, badDateString, "DateTime"); // Still says it's bad.
// TypeConverter.IsValid(badDateString) returns false, so we shouldn't be able to convert it.
// Well, we can, like so:
dateTimeValue = (DateTime)dateConverter.ConvertFrom(badDateString); // Shouldn't work according to "IsValid()"
Console.WriteLine("dateTimeValue (bad) = " + dateTimeValue); // But this is printed ok.
// TypeConverter.IsValid(goodDateString) returns true, so we can convert it right?
// Well, no. This now throws an exception, even though "IsValid()" returned true for the same string.
dateTimeValue = (DateTime)dateConverter.ConvertFrom(goodDateString); // This throws an exception.
}
void test(TypeConverter converter, string text, string type)
{
if (converter.IsValid(text))
Console.WriteLine("\"" + text + "\" IS a valid " + type);
else
Console.WriteLine("\"" + text + "\" is NOT a valid " + type);
}
static void Main()
{
new Program().Run();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是 TypeConverter.IsValid() 中的一个缺陷。类型转换器具有文化感知能力,其虚拟 ConvertFrom() 方法采用 CultureInfo 参数。您正在使用不带 CultureInfo 的非虚拟重载,因此您将获得由 CultureInfo.CurrentCulture 选择的转换。
TypeConverter.IsValid()不不具有接受的CultureInfo的过载。并清除它传递给 TypeConverter.ConvertFrom() 方法的 CultureInfo,它传递 CultureInfo.InvariantCulture。
很难为这种行为找到理由。可能是一个刚刚被发现太晚而无法做任何事情的错误。肯定不能再修了。
解决方法是执行 IsValid() 执行的操作,但指定正确的区域性。首先调用 CanConvertFrom(),然后在 try/catch 块中调用 ConvertFrom()。
| 归档时间: |
|
| 查看次数: |
1057 次 |
| 最近记录: |