gpt4 book ai didi

c# - 使用 CultureInfo 发出警告的代码

转载 作者:行者123 更新时间:2023-11-30 21:51:58 25 4
gpt4 key购买 nike

我有一个代码可以向我显示如下警告:

Because the behavior of 'string.Format(string, object, object)' could vary based on the current user's locale settings, replace this call in 'MethodName' with a call to 'string.Format(IFormatProvider, string, params object[])'. If the result of 'string.Format(IFormatProvider, string, params object[])' will be displayed to the user, specify 'CultureInfo.CurrentCulture' as the 'IFormatProvider' parameter. Otherwise, if the result will be stored and accessed by software, such as when it is persisted to disk or to a database, specify 'CultureInfo.InvariantCulture'.

它的代码是

string[] l_array = (from key in nameValueCollection.AllKeys
from value in nameValueCollection.GetValues(key)
select $"{(key)}={(value)}").ToArray();
return string.Join("&", l_array);

我想我可以写文化信息,但我在哪里以及为什么需要它?或者我还应该更改什么。

最佳答案

编辑,看起来 $"{{key}}={{value}}".ToString(CultureInfo.InvariantCulture) 不起作用,我已经更新了我的解决方案。

您可以使用以下方法删除此警告:

string[] l_array = (from key in nameValueCollection.AllKeys
from value in nameValueCollection.GetValues(key)
select InvariantCulture($"{(key)}={(value)}"))
.ToArray();


...
public static string InvariantCulture(IFormattable formattable)
{
return formattable.ToString(null, CultureInfo.InvariantCulture);
}

关于c# - 使用 CultureInfo 发出警告的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35091969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com