gpt4 book ai didi

c# - 将哈希表更改为字典

转载 作者:太空宇宙 更新时间:2023-11-03 13:14:23 24 4
gpt4 key购买 nike

我已经读过 this .但是没有例子可以让它发挥作用。所以我自己试了一下。这是我的代码:

public void AskServer(List<Kvp> kvps)
{
WWWForm form = new WWWForm();
Hashtable headers = form.headers;
if (this._lastCookies != string.Empty) {
headers.Add("Cookie", this._lastCookies);
}
foreach (var arg in kvps) {
form.AddField(arg.Key, arg.Value.ToString());
}
form.AddField("pseudo", this._pseudo);
form.AddField("jeton", this._dernierJeton.ToString());
StartCoroutine(SendToServer(
new WWW(this._URL, form.data, headers)
));
}

现在,有一条警告说调用新的 WWW(this._URL, form.data, headers)已经过时了,我应该用字典里的那个。声明是这样的:

    public WWW(string url, byte[] postData, Dictionary<string, string> headers);
[Obsolete("This overload is deprecated. Use the one with Dictionary argument.")]
public WWW(string url, byte[] postData, Hashtable headers);

因此,当我尝试使用问题开头提供的链接中的示例时,我有这样的代码,它不起作用:

public static Dictionary<K, V> HashtableToDictionary<K, V>(Hashtable table)
{
return table
.Cast<DictionaryEntry>()
.ToDictionary(kvp => (K)kvp.Key, kvp => (V)kvp.Value);
}

public void AskServer(List<Kvp> kvps)
{
WWWForm form = new WWWForm();
Dictionary<string, string> headers = StateManager.HashtableToDictionary<string, object>(form.headers);
if (this._lastCookies != string.Empty) {
headers.Add("Cookie", this._lastCookies);
}
foreach (var arg in kvps) {
form.AddField(arg.Key, arg.Value.ToString());
}
form.AddField("pseudo", this._pseudo);
form.AddField("jeton", this._dernierJeton.ToString());
StartCoroutine(SendToServer(
new WWW(this._URL, form.data, headers)
));
}

错误是:Assets/Code/StateManager.cs(58,36): error CS0029: Cannot implicitly convert type System.Collections.Generic.Dictionary' 到 System.Collections.Generic.Dictionary<string,string>'

我做错了什么?有没有更有效的方法来做到这一点?

最佳答案

问题是您正在尝试分配 Dictionary<string, object>Dictionary<string, string> 类型的变量.为了解决问题更改

Dictionary<string, string> headers = 
StateManager.HashtableToDictionary<string, object>(form.headers);

Dictionary<string, object> headers = 
StateManager.HashtableToDictionary<string, object>(form.headers);

Dictionary<string, string> headers = 
StateManager.HashtableToDictionary<string, string>(form.headers);

关于c# - 将哈希表更改为字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27080694/

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