简体   繁体   English

什么“中立文化”打破DateTime.TryParse()?

[英]What “neutral culture” breaks DateTime.TryParse()?

I have the following code snippet 我有以下代码片段

DateTime date1;
CultureInfo neutralCulture = new CultureInfo("fr");
bool isNeutral = neutralCulture.IsNeutralCulture; // True

DateTime.TryParse("not a date", neutralCulture, DateTimeStyles.AdjustToUniversal, out date1);

Which executes without throwing an exception, however, the documentation states 但是, 文档指出,它在不抛出异常的情况下执行

NotSupportedException: provider is a neutral culture and cannot be used in a parsing operation. NotSupportedException: provider一种中性文化,不能用于解析操作。

"fr" is a neutral culture, as demonstrated by the property on the CultureInfo object above, so I would expect this code to break. "fr"一种中性文化,正如上面CultureInfo对象上的属性所证明的那样,所以我希望这段代码能够破解。

What "neutral culture" breaks this method - is this documented anywhere? 什么“中立文化”打破了这种方法 - 这在任何地方都有记录吗?

I did a quick test of all the neutral cultures, and not even one threw an exception as shown below: 我对所有中性文化进行了快速测试,甚至没有人抛出异常,如下所示:

CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.NeutralCultures);
foreach (var thisCulture in cultures)
{
    DateTime date1;
    CultureInfo neutralCulture = new CultureInfo(thisCulture.Name);
    bool isNeutral = neutralCulture.IsNeutralCulture; // True

    DateTime.TryParse("not a date", neutralCulture, DateTimeStyles.AdjustToUniversal, out date1);
}

I am not sure what to conclude from that. 我不确定从中得出什么结论。 Could the documentation be wrong? 文件可能有误吗? Who knows. 谁知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM