2014-11-22 74 views
2

我已閱讀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>'

我在做什麼錯?有沒有更有效的方法呢?

+0

我不知道,如果我們能做到這一點與Unityscript ... – cregox 2014-12-11 19:01:24

回答

2

問題是,您正嘗試將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); 
+0

好,但現在我有另一個錯誤:'資產/代碼/ StateManager.cs(51,12):錯誤CS1061:類型'System.Collections.Hashtable'不包含'鑄造'的定義和沒有擴展方法'鑄造'的可以找到'System.Collections.Hashtable'類型(你是否缺少使用指令或程序集引用?)' – 2014-11-22 18:38:31

+0

@OivierPons在文件的頂部添加'using System.Linq;'。 – dotnetom 2014-11-22 18:40:51

+0

它的工作原理! **'\ o /'**奇怪的是,當我右鍵單擊錯誤時,沒有「添加使用」選項,就像在這種情況下顯示的那樣。對不起,我的英語不好,再次非常感謝! – 2014-11-22 18:46:30

-1

好像在unit3d的文檔需要更新的wwwform代碼,看到它仍然顯示一個具有完全相同問題的例子。

我在上面找到的解決方案首先遇到的問題是,當您查看www引用時,www類是否需要散列表作爲第三個參數。它似乎那麼,這一解決方案將工作:

Hashtable header = new Hashtable(form.headers); 

與唯一的問題是,使用哈希表是過時的,所以你確實有使用字典。只要記住,你可能還需要再加入

using System.Collections.Generic; 
0

對於任何人誰可能下跌在這個問題(一個最小的例子):

//make a short dictionary (only one Entry - remember to set also the 
    //appropriate Length if you want to submit non Text Data 

    Dictionary<string, string> headers = new Dictionary<string, string>(); 
    headers.Add("Content-Type", "application/x-www-form-urlencoded"); 

     //ADD your Form Elements wich you want to transmit 
     WWWForm form = new WWWForm(); 
     form.AddField("ChunkCoordinate", ChunkCoordinate);   // 0/0 

     byte[] rawData = form.data; 
     string url = "http://yourURL.com/addSomething.php?"; 

     // Post a request to an URL with our custom headers 
     WWW www = new WWW(url, rawData, headers);