我已閱讀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)
));
}
現在,有一個警告說調用new WWW(this._URL, form.data, headers)
已經過時了,我應該使用一個與dictionnary。該聲明是這樣的:
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>'
我在做什麼錯?有沒有更有效的方法呢?
我不知道,如果我們能做到這一點與Unityscript ... – cregox 2014-12-11 19:01:24