问题
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