我已经读过了。但是,没有任何实例可以使它起作用。所以我自己尝试了。这是我的代码:
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)
));
}
现在,有一个警告说,调用newWWW(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>
给type变量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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句