2010-01-27 54 views
2

昨天我問了這個questionRubens Farias指向他寫的這個piece of code回答。它下面的部分不能由MS的Visual Studio 2010專業版Beta 2中解釋一塊C#代碼並轉換爲VB.NET

byte[] buffer = 
Encoding.UTF8.GetBytes(
    String.Join("&", 
     Array.ConvertAll<KeyValuePair<string, string>, string>(
      inputs.ToArray(), 
      delegate(KeyValuePair item) 
      { 
       return item.Key + "=" + HttpUtility.UrlEncode(item.Value); 
      }))); 

它給在Visual Studio these錯誤進行編譯。不幸的是Rubens不再回復。

所以,我有以下幾個問題/請求:

  1. 我不明白這一段代碼,請解釋究竟是什麼發生。
  2. 請解釋如何改寫這部分,以便它在VS中「工作」。
  3. 請解釋我應該如何將其轉換爲VB.NET。我已經嘗試使用在線轉換器無濟於事。
+0

恩,你說的是魯本斯嗎? :) http://stackoverflow.com/users/113794/rubens-farias – 2010-01-27 09:38:23

+0

是的,這就是他。 – George 2010-01-27 09:43:17

回答

4
  • KeyValuePair需要兩個類型參數。在你的委託聲明中,它簡單地說是KeyValuePair item,沒有類型參數。將其更改爲delegate(KeyValuePair<string,string> item)
  • HttpUtilitySystem.Web命名空間中聲明;將using System.Web;添加到文件開頭的using語句中。

個人而言,我覺得它更容易和更清潔的使用拉姆達樣式這種代碼:

byte[] buffer = 
    Encoding.UTF8.GetBytes(
     String.Join("&", 
      Array.ConvertAll<KeyValuePair<string, string>, string>(
       inputs.ToArray(), (item) => item.Key + "=" + HttpUtility.UrlEncode(item.Value)))); 

一旦你得到的C#代碼工作,DeveloperFusion C# to VB.NET轉換器做這項工作:

' Converted from delegate style C# implementation ' 
Dim buffer As Byte() = Encoding.UTF8.GetBytes(_ 
    [String].Join("&", _ 
    Array.ConvertAll(Of KeyValuePair(Of String, String), String)(inputs.ToArray(), _ 
     Function(item As KeyValuePair(Of String, String)) (item.Key & "=") + HttpUtility.UrlEncode(item.Value)))) 

' Converted from Lambda style C# implementation ' 
Dim buffer As Byte() = Encoding.UTF8.GetBytes(_ 
    [String].Join("&", _ 
    Array.ConvertAll(Of KeyValuePair(Of String, String), String)(inputs.ToArray(), _ 
     Function(item) (item.Key & "=") + HttpUtility.UrlEncode(item.Value)))) 
+0

+1,ty Fredrik! – 2010-01-27 09:35:29

+0

我仍然無法使它工作。它說「名稱'HttpUtility'沒有被聲明,我想它只適用於ASP.NET項目?我試着添加一個對system.web和system.web.dll的引用,但沒有去。建議:「使用System.Web」,我已經擁有它 – George 2010-01-27 09:46:31

+0

@iar:year必須確保代碼所在的項目有一個對程序集「System.Web」的引用,並且有一個using(或者Imports在VB.NET中)語句在代碼文件中導入名稱空間using/Imports語句的替代方法是在代碼中限定名稱('System.Web.HttpUtility.UrlEncode(...')。添加一個引用到'System.Web'程序集雖然 – 2010-01-27 09:50:37

1
byte[] buffer = 
Encoding.UTF8.GetBytes(
    String.Join("&", 
     Array.ConvertAll<KeyValuePair<string, string>, string>(
      inputs.ToArray(), 
      delegate(KeyValuePair<string, string> item) 
      { 
       return item.Key + "=" + System.Web.HttpUtility.UrlEncode(item.Value); 
      }))); 

試試看。

  1. 該代碼似乎正在構建GET請求列表項,例如, key1=value1&key2=value2。這是通過首先將inputs陣列轉換成單個元素key=value,然後String.Join將它們與&符號一起進行。然後它返回數組中的UTF8字節。

  2. 這有效(見代碼)。

  3. 我不是一個VB.NET程序員,對不起,但我會在第二時間去。

+0

弗雷德裏克·莫克在轉換它方面做得很好。我放棄了,嘿。 – Codesleuth 2010-01-27 09:16:49

+0

無論如何感謝您的解釋。 – George 2010-01-27 09:48:39

1

它含有鍵/值對的輸入列表轉換成看起來很像一個查詢字符串的字符串(例如,ITEM1 =值1 & ITEM2 =值2),則轉換到這一點使用UTF8緩衝器字節數組編碼。

Public Class _Default 
    Inherits System.Web.UI.Page 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     Dim inputs As New List(Of KeyValuePair(Of String, String)) 
     inputs.Add(New KeyValuePair(Of String, String)("a", "adata")) 

     Dim buffer As Byte() = _ 
      Encoding.UTF8.GetBytes(_ 
       String.Join("&", _ 
       Array.ConvertAll(Of KeyValuePair(Of String, String), String)(_ 
        inputs.ToArray(), _ 
        Function(item As KeyValuePair(Of String, String)) _ 
        item.Key & "=" & HttpUtility.UrlEncode(item.Value) _ 
       ))) 
    End Sub 
End Class 
+0

謝謝,但它給出了相同的錯誤消息。 – George 2010-01-27 09:48:05

+0

看起來像Fredrik對你來說有最後一塊難題......只要確保你在文件頂部有「Imports System.Web」。這可能會清除錯誤。 – Will 2010-01-27 09:57:37

+0

我有「Imports System.Web」,但它並沒有消除「Name'HttpUtility'沒有聲明」的錯誤。 – George 2010-01-27 10:18:21