Invalid CultureInfo no longer throws CultureNotFoundException

大憨熊 提交于 2020-04-10 07:14:28

问题


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

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?

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

Update 1

The documentation mentions the following:

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.

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


回答1:


Now add an answer based on the comments.

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.

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

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.



来源:https://stackoverflow.com/questions/35074033/invalid-cultureinfo-no-longer-throws-culturenotfoundexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!