.NET Core/6/7 supports a globalization-invariant mode which alters the behavior regarding globalization in many ways.
How can a library detect if it is running in this mode and adjust its behaviour accordingly?
The only solution I came up so far is to use the fact, that since .NET 6 creating a culture which is not the invariant culture in this mode will (according to this document) throw a CultureNotFoundException
.
bool IsGlobalizationInvariantModeEnabled()
{
try
{
_ = new CultureInfo("en-US");
return false;
}
catch (CultureNotFoundException)
{
return true;
}
}
I do not like the solution because it abuses exceptions and also assumes that the "en-US" culture is always available if the globalization-invariant mode is not activated.
Is there a better way?