无效的CultureInfo不再引发CultureNotFoundException [英] Invalid CultureInfo no longer throws CultureNotFoundException

查看:121
本文介绍了无效的CultureInfo不再引发CultureNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用es-CA创建文化信息,显然是不正确的,应该抛出异常,但不再这样做.

Creating a culture info with es-CA, which obviously is incorrect should throw an exception, but no longer does.

以前抛出了CultureNotFoundException:new CultureInfo("es-CA").现在,它似乎回落到es并带有未知语言环境".虽然,执行xy-ZZ之类的方法也可以,但这很奇怪吗?

This previously threw a CultureNotFoundException: new CultureInfo("es-CA"). It now seem to fall back to es with an "Unknown Locale". Although, doing something like xy-ZZ also works, which it's rather odd?

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

Why does this no longer throw an exception? Was this changed in a recent version of .NET?

更新1

文档中提到以下内容:

如果操作系统不支持该区域性,并且如果name不是补充或替代区域性的名称,则该方法将引发CultureNotFoundException异常.

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上不会抛出异常.

Testing this on Windows 7, it throws CultureNotFoundException but on Windows 10 it does not throw the exception.

推荐答案

现在根据评论添加答案.

Now add an answer based on the comments.

由于Windows设计的变化,如果名称与BCP-47匹配,则不再存在无效区域性",因此,不要抛出异常.NETFramework/.NET Core会接受新区域性.

Due to the changes in Windows design, now there is no more "invalid culture" if the name matches BCP-47, so instead of throwing an exception .NET Framework/.NET Core accept the new culture.

您可以参考 GitHub讨论,以及以下引用

You can refer to the GitHub discussion, and the below quote,

由于框架依赖于操作系统来获取文化,因此操作系统的 正在将模型转换为任何BCP-47文化名称,即使 操作系统不支持它.例如,Windows 10支持任何 格式正确的文化名称,即使操作系统也没有此类的真实数据 文化.例如,如果尝试在 Windows 10将成功.考虑到这一点, 具有文化枚举,因为我们返回的任何集合并不意味着这些是 唯一支持的文化.看着你的问题,你的解决方法是 可以接受的.如果真的想有更好的方法,我们可以考虑 像CultureInfo.TryGetCulture()一样,但是正如我之前所说 推进几乎任何文化都是有效的.

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.

这篇关于无效的CultureInfo不再引发CultureNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆