开发者

CultureNotFoundException: Only the invariant culture is supported in globalization-invariant mode

I created a simple console app in.NET6 to deal with decimals.

My project setting must be like this: (it should be <InvariantGlobalization>true</InvariantGlobalization>)

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <InvariantGlobalization>true</InvariantGlobalization>
  </PropertyGroup>

and my program.cs file is like this:

using System.Globalization;

public static class Program
{
    private static readonly CultureInfo DecimalSeparator = new CultureInfo("en-US")
    {
        NumberFormat = new NumberFormatInfo { CurrencyDecimalSeparator = ".", CurrencyGroupSeparator = "," }
    };

    public static void Main(string[] args)
    {
        var mycultureInfo = DecimalSeparator;
    }
}

After running the program, I got an exception:

System.TypeInitializationException: 'T开发者_运维百科he type initializer for 'Program' threw an exception.'
CultureNotFoundException: Only the invariant culture is supported in globalization-invariant mode. See https://aka.ms/GlobalizationInvariantMode for more information.

All the suggested solutions those I found on the internet saying to use

<InvariantGlobalization>false</InvariantGlobalization>

Is there any around work/idea to solve this issue without setting InvariantGlobalization to false. Because changing InvariantGlobalization to be false can lead to many unexpected consequences.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜