【问题标题】:Explain piece of C# code and conversion to VB.NET解释一段 C# 代码并转换为 VB.NET
【发布时间】:2010-01-27 08:50:28
【问题描述】:

昨天我问了这个questionRubens Farias 指着他写的piece of code 回答了这个问题。 MS Visual Studio 2010 Professional Beta 2无法编译以下部分。

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 错误。不幸的是,鲁本斯不再回复了。

所以我有以下问题/要求:

  1. 我不明白这段代码,请解释一下到底发生了什么。
  2. 请说明如何重写此部分以使其在 VS 中“工作”。
  3. 请解释我应该如何将它转换为 VB.NET。我使用在线转换器尝试过,但无济于事。

【问题讨论】:

标签: c# vb.net compiler-construction


【解决方案1】:
  • KeyValuePair 需要两个类型参数。在您的委托声明中,它只是简单地说KeyValuePair item,没有类型参数。将此更改为delegate(KeyValuePair&lt;string,string&gt; item)
  • HttpUtilitySystem.Web 命名空间中声明;在文件开头的 using 语句中添加using System.Web;

我个人觉得在这种代码中使用 lambda 样式更容易、更简洁:

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))))

【讨论】:

  • 我仍然无法让它工作。它说“未声明名称'HttpUtility'。我猜它仅适用于 ASP.NET 项目?我尝试添加对 system.web 和 system.web.dll 的引用,但不行。你的也一样建议:“使用 System.Web”,我已经有了。
  • @iar: yoy 必须确保代码所在的项目具有对程序集 System.Web 的引用,并且有一个 using(或 VB.NET 中的 Imports)语句来导入代码文件中的命名空间。 using/Imports 语句的替代方法是在代码中限定名称 (System.Web.HttpUtility.UrlEncode(...)。不过,您仍然需要添加对 System.Web 程序集的引用。
  • 它说“'HttpUtility'不是'Web'的成员”。我现在有这行代码:System.Web.HttpUtility.UrlEncode(item.Value)。文件顶部还有“Imports System.Web”。在解决方案资源管理器中使用添加引用添加引用时,.NET 选项卡中没有 System.Web。正如你现在已经注意到的那样,我还不是一个很有经验的用户;)
  • @iar:这可能与所选的目标框架有关(如果目标框架是“.NET Framework 4 Client Profile”,则 System.Web 不可用)。解决方法(对于 VB.NET 项目):双击“我的项目”,选择“编译”选项卡,单击“高级编译选项”按钮(页面最下方)并将目标框架更改为“.NET Framework 4” .
【解决方案2】:
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&amp;key2=value2。这是通过首先将inputs 数组转换为key=value 的单个元素然后String.Join 将它们与一个& 符号一起转换来完成的。然后它在一个数组中返回 UTF8 字节。

  2. 这可行(见代码)。

  3. 对不起,我不是 VB.NET 程序员,但我马上试试。

【讨论】:

  • Fredrik Mörk 在转换方面做得很好。我放弃了,呵呵。
  • 感谢您的解释。
【解决方案3】:

它将包含键/值对的输入列表转换为一个看起来很像查询字符串的字符串(例如 item1=value1&item2=value2),然后使用 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

【讨论】:

  • 看起来 Fredrik 已经为您解决了最后的难题……只要确保文件顶部有“Imports System.Web”即可。这可能会清除错误。
  • 我有“Imports System.Web”,但它并没有消除“未声明名称'HttpUtility'”的错误。
  • 在我将第二次提到的“KeyValuePair”更正为“KeyValuePair(Of String, String)”之后,它在这里编译得很好。我更新了转换后的代码,使其显示完整的类。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-17
  • 2012-02-09
相关资源
最近更新 更多