解释件C#代码和代码转换到VB.NET
-
23-09-2019 - |
题
昨天我问这个 的问题. 鲁本斯Farias 回答说:它通过向这个 一块代码 他写的。下列的一部分,它无法汇编MS Visual Studio2010年专业测试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.不幸的是鲁本斯不会回复了。
所以我下面的问题/要求:
- 我不明白这段代码,请解释发生了什么准确。
- 请解释如何这一部分已te以改写,以便于它到"工作"VS。
- 请解释如何我应该把这转换为VB.NET.我已经尝试过使用在线转换无济于事。
解决方案
- 型需要两个类型的论点。在你的委托宣言》,它简单地说
KeyValuePair item
, 没有类型参数。改变这个delegate(KeyValuePair<string,string> item)
HttpUtility
声明中System.Web
名字空间;添加using System.Web;
使用发言开始的文件。
我个人觉得更容易和更清洁的使用氧式为这样的代码:
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#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))))
其他提示
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);
})));
尝试。
代码似乎是建立一个得到请求的项目列表如
key1=value1&key2=value2
.这是通过第一个转换的inputs
阵列入单个元素的key=value
然后String.Join
ing他们一起与一个符号.然后它返回UTF8字节中的阵列。这一工作(见码)。
我不是一个VB.NET 程序员,对不起,但是我会去的第二个。
这是转换的投入,列出包含关键值对成一串,看起来很像查询string(eg.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
不隶属于 StackOverflow