【问题标题】:C# How to create a generic List of KeyValuePairC# 如何创建 KeyValuePair 的通用列表
【发布时间】:2019-03-14 21:26:23
【问题描述】:

我创建了一个 KeyValuePair 列表来填充内容作为 HttpClient 的数据。

List<KeyValuePair<string, string>> keyValues = new List<KeyValuePair<string, string>>();

keyValues.Add(new KeyValuePair<string, string>("email", email));
keyValues.Add(new KeyValuePair<string, string>("password", password));
keyValues.Add(new KeyValuePair<string, string>("plan_id", planId));

var content = new FormUrlEncodedContent(keyValues);

但后来我发现我必须发送一个 int 值作为 plan_id。如何更改上述列表以接受 KeyValuePair。或者有没有更好的方法来做到这一点?

【问题讨论】:

  • 最终,HTTP 中的所有内容都作为 ASCII 字符串发送,那么为什么不直接调用 planId.ToString(CultureInfo.InvariantCulture)
  • 我正在调用外部 API。当我为计划 ID 发送字符串值时,我收到了错误请求。但有了 int 值,它就成功了。
  • 是否可以在 java 中创建如下通用列表。 List> keyValues = new List>();
  • 好吧,FormUrlEncodedContent 只会接受 IEnumerable&lt;KeyValuePair&lt;string, string&gt;&gt; nameValueCollection,所以除非您对内容进行不同的编码,否则您必须这样做,正如@Dai 所提到的,将您的整数转换为其字符串表示形式。
  • 感谢@Dai 和 Alex.. 这行得通..

标签: c# list generics httpclient keyvaluepair


【解决方案1】:

如果你想创建一个KeyValuePair 列表,你应该创建字典。

Dictionary<string, string> dic = new Dictionary<string,string>();
dic.Add("email", email);
dic.Add("password", password);
dic.Add("plan_id", planId.ToString());

【讨论】:

  • 这里需要创建的是这样的东西。就像 java 中的通用 ArrayList 一样。 List> keyValues = new List>();
【解决方案2】:

使用KeyValuePair&lt;string, object&gt; 放置值并创建转换 列表到KeyValuePair&lt;string, string&gt; 何时使用FormUrlEncodedContent

创建一个新列表

List<KeyValuePair<string, object>> keyValues = new List<KeyValuePair<string, object>>();

keyValues.Add(new KeyValuePair<string, object>("email", "asdasd"));
keyValues.Add(new KeyValuePair<string, object>("password", "1131"));
keyValues.Add(new KeyValuePair<string, object>("plan_id", 123));
keyValues.Add(new KeyValuePair<string, object>("other_field", null));

var content = new FormUrlEncodedContent(keyValues.Select(s => 
    new KeyValuePair<string, string>(s.Key, s.Value != null ? s.ToString() : null)
));

转换列表

public static KeyValuePair<string, string> ConvertRules(KeyValuePair<string, object> kv)
{
    return new KeyValuePair<string, string>(kv.Key, kv.Value != null ? kv.ToString() : null);
}

static Task Main(string[] args) 
{
    List<KeyValuePair<string, object>> keyValues = new List<KeyValuePair<string, object>>();

    keyValues.Add(new KeyValuePair<string, object>("email", "asdasd"));
    keyValues.Add(new KeyValuePair<string, object>("password", "1131"));
    keyValues.Add(new KeyValuePair<string, object>("plan_id", 123));
    keyValues.Add(new KeyValuePair<string, object>("other_field", null));

    var content = new FormUrlEncodedContent(keyValues.ConvertAll(ConvertRules)));
));

【讨论】:

    【解决方案3】:

    不要使用List&lt;KeyValuePair&lt;string,string&gt;&gt;,而不是Dictionary&lt;string, string&gt;。使用 planId.ToString()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-06
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多