gpt4 book ai didi

c# - TypeConverter.IsValid() 使用当前线程区域性但 TypeConverter.ConvertFrom() 不使用?

转载 作者:行者123 更新时间:2023-11-30 15:33:53 25 4
gpt4 key购买 nike

似乎 TypeConverter.IsValid() 使用了当前的线程文化,但 TypeConverter.ConvertFrom() 没有。

这使得将 TypeConverter.IsValid()DateTime 类型一起使用变得毫无用处,除非您处于不变文化中。确实,这似乎是一个错误。

有谁知道如何让 TypeConverter.IsValid() 使用当前文化?

下面的代码演示了这个问题。

它使用两个字符串,一个是 DD/MM/YYYY 格式,一个是 MM/DD/YYYY 格式。

测试的第一部分是在不变文化中完成的。它演示了 TypeConverter.IsValid() 为 MM/DD/YYYY 字符串返回 true 并且您可以使用 TypeConverter.ConvertFrom() 将该字符串转换为 日期时间

第一部分还演示了 TypeConverter.IsValid() 为 DD/MM/YYYY 字符串返回 false。

对于第二部分,我更改了使用“DD/MM/YYYY”日期的当前文化“en-GB”。

我现在希望 IsValid() 结果反转,但它返回的两个字符串与以前相同。所以它说 MM/DD/YYYY 字符串是有效的。

但是 - 这是重要的一点 - 如果您尝试使用 TypeConverter.ConvertFrom() 实际转换 TypeConverter.IsValid() 所说的字符串是可以的, 你会得到一个异常(exception)。

有没有办法解决这个问题?

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();
}
}
}

最佳答案

这是 TypeConverter.IsValid() 中的一个缺陷。类型转换器具有文化意识,其虚拟 ConvertFrom() 方法采用 CultureInfo 参数。您正在使用不采用 CultureInfo 的非虚拟重载,因此您将获得 CultureInfo.CurrentCulture 选择的转换。

TypeConverter.IsValid() 有接受 CultureInfo 的重载。并将传递给 TypeConverter.ConvertFrom() 方法的 CultureInfo 弄乱,它传递 CultureInfo.InvariantCulture。

很难为这种行为找到理由。可能是一个发现得太晚而无法采取任何措施的错误。它肯定无法再修复。

解决方法是执行 IsValid() 的操作,但要指定正确的区域性。首先调用 CanConvertFrom(),然后在 try/catch block 中调用 ConvertFrom()。

关于c# - TypeConverter.IsValid() 使用当前线程区域性但 TypeConverter.ConvertFrom() 不使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16837774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com