gpt4 book ai didi

c# - 无效的 CultureInfo 不再抛出 CultureNotFoundException

转载 作者:可可西里 更新时间:2023-11-01 08:48:14 25 4
gpt4 key购买 nike

使用 es-CA 创建文化信息,显然不正确应该抛出异常,但现在不会。

之前抛出了一个CultureNotFoundException:new CultureInfo("es-CA")。它现在似乎退回到 es 并带有“Unknown Locale”。虽然,像 xy-ZZ 这样的操作也有效,但这很奇怪?

为什么这不再抛出异常?这在最新版本的 .NET 中是否发生了变化?

更新 1

文档提到了以下内容:

if the operating system does not support that culture, and if name is not the name of a supplementary or replacement culture, the method throws a CultureNotFoundException exception.

在 Windows 7 上测试它,它抛出 CultureNotFoundException 但在 Windows 10 上它不会抛出异常。

最佳答案

现在根据评论添加答案。

由于 Windows 设计的变化,如果名称匹配 BCP-47,现在不再有“无效文化”,因此 .NET Framework/.NET Core 不会抛出异常,而是接受新文化。

可以引用the GitHub discussion ,以及下面的引述,

As the framework depends on the OS for getting the cultures, the OS's is moving to the model any BCP-47 culture name become valid even the OS is not supporting it. for instance, Windows 10 is supporting any well formed culture name even the OS don't have real data for such culture. for example, if trying to create a culture "xx-XXXX" in Windows 10 it will succeed. considering that, it doesn't make sense to have culture enumeration as any set we return doesn't mean these are the only supported cultures. looking at your issue, you workaround is acceptable. if really want to have more better way, we can consider something like CultureInfo.TryGetCulture() but as I said previously moving forward almost any culture will be valid.

关于c# - 无效的 CultureInfo 不再抛出 CultureNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35074033/

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