C# 解释一段C代码和到VB.NET的转换
昨天我问了这个问题。他指着他写的这个回答。以下部分无法由MS Visual Studio 2010 Professional Beta 2编译C# 解释一段C代码和到VB.NET的转换,c#,vb.net,compiler-construction,C#,Vb.net,Compiler Construction,昨天我问了这个问题。他指着他写的这个回答。以下部分无法由MS Visual Studio 2010 Professional Beta 2编译 byte[] buffer = Encoding.UTF8.GetBytes( String.Join("&", Array.ConvertAll<KeyValuePair<string, string>, string>( inputs.ToArray(),
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);
})));
byte[]缓冲区=
Encoding.UTF8.GetBytes(
String.Join(&),
Array.ConvertAll(
inputs.ToArray(),
委托(KeyValuePair项)
{
返回item.Key+“=”+HttpUtility.UrlEncode(item.Value);
})));
它在VisualStudio中提供错误。不幸的是,鲁本斯不再回答了
因此,我有以下问题/要求:
byte[]缓冲区=
Encoding.UTF8.GetBytes(
String.Join(&),
Array.ConvertAll(
inputs.ToArray(),
委托(KeyValuePair项)
{
返回item.Key+“=”+System.Web.HttpUtility.UrlEncode(item.Value);
})));
试试看
key1=value1&key2=value2
。首先将输入
数组转换为键=值
的单个元素,然后转换为字符串。用一个符号将它们连接在一起。然后返回数组中的UTF8字节
它将包含键/值对的输入列表转换为一个看起来很像查询字符串的字符串(例如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
- KeyValuePair需要两个类型参数。在委托声明中,它只显示
,没有类型参数。将此项更改为keyvaluepairitem
委托(KeyValuePair项)
在HttpUtility
命名空间中声明;使用System.Web添加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))));
弗雷德里克·莫尔克(Fredrik Mörk)很好地实现了这一转变。我要放弃了,呵呵。无论如何谢谢你的解释。嗯,你是说这个鲁本斯吗?:)我还是不能让它工作。它说“名称‘HttpUtility’未声明。我想它仅适用于ASP.NET项目?我已尝试添加对system.web和system.web.dll的引用,但没有成功。您的建议也是这样:“使用system.web。”“,我已经有了它。@iar:yoy必须确保代码所在的项目引用了程序集
System.Web
,并且有一个using(或在VB.NET中导入)语句来导入代码文件中的命名空间。using/Imports语句的另一种替代方法是限定代码(System.Web.HttpUtility.UrlEncode(…
)中的名称。不过,您仍然需要添加对System.Web
程序集的引用。它说“'HttpUtility'不是'Web'的成员”。我现在有这行代码:System.Web.HttpUtility.UrlEncode(item.Value)。还有“导入文件顶部的System.Web”。在解决方案资源管理器中使用“添加引用”添加引用时,.NET选项卡中没有System.Web。正如您现在注意到的,我还不是一个非常有经验的用户;)@iar:这可能与所选的目标框架有关(如果目标框架不可用,则System.Web不可用).NET Framework 4客户端配置文件)。要解决此问题(对于VB.NET项目):双击“我的项目”,选择“编译”选项卡,单击“高级编译选项”按钮(位于页面下方)并将目标框架更改为“.NET Framework 4”。看起来Fredrik为您解决了最后一个难题…请确保您有“Imports System.Web”“在文件的顶部。这可能会清除错误。我有“Imports System.Web”,但它并没有删除“Name'HttpUtility'未声明”的错误。在我将第二次提到的“KeyValuePair”改为“KeyValuePair(of String,String)”后,它在这里编译得很好。我更新了转换后的代码,以便它显示完整的类。
byte[] buffer =
Encoding.UTF8.GetBytes(
String.Join("&",
Array.ConvertAll<KeyValuePair<string, string>, string>(
inputs.ToArray(), (item) => item.Key + "=" + HttpUtility.UrlEncode(item.Value))));
' 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))))