从 C# 转换为 vb.net 后,它显示错误
Posted
技术标签:
【中文标题】从 C# 转换为 vb.net 后,它显示错误【英文标题】:After converting from C# to vb.net it shows an error 【发布时间】:2020-07-09 19:17:35 【问题描述】:我正在尝试将 C# 代码转换为 Visual Basic。
这是 C# 代码:
public static string RequestServer1(string methodName, List<string> parameters)
return RequestServer(methodName, parameters.Select(p => new JValue(p)).ToList<JToken>());
这是 VB.Net 代码:
Public Shared Function RequestServer(ByVal methodName As String, ByVal parameters As List(Of String)) As String
Return RequestServer(methodName, parameters.Select(Function(p) New JValue(p)).ToList())
End Function
这里是List(Of JToken)
的函数:
Public Shared Function RequestServer(ByVal methodName As String, ByVal parameters As List(Of JToken)) As JToken
Dim ServerIp As String = "http://localhost:8332"
Dim UserName As String = "hama"
Dim Password As String = "hama"
Dim webRequest As HttpWebRequest = CType(webRequest.Create(ServerIp), HttpWebRequest)
webRequest.Credentials = New NetworkCredential(UserName, Password)
webRequest.ContentType = "application/json-rpc"
webRequest.Method = "POST"
Dim respVal As String = String.Empty
Dim joe As JObject = New JObject
joe.Add(New JProperty("jsonrpc", 1))
joe.Add(New JProperty("id", 1))
joe.Add(New JProperty("method", methodName))
Dim props As JArray = New JArray
For Each parameter In parameters
props.Add(parameter)
Next
joe.Add(New JProperty("params", props))
' serialize json for the request
Dim s As String = JsonConvert.SerializeObject(joe)
Dim byteArray() As Byte = Encoding.UTF8.GetBytes(s)
webRequest.ContentLength = byteArray.Length
Dim dataStream As Stream = webRequest.GetRequestStream
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim streamReader As StreamReader = Nothing
Try
Dim webResponse As WebResponse = webRequest.GetResponse
streamReader = New StreamReader(webResponse.GetResponseStream, True)
respVal = streamReader.ReadToEnd
Dim data = JsonConvert.DeserializeObject(respVal).ToString
Return data
Catch exp As Exception
Finally
If (Not (streamReader) Is Nothing) Then
streamReader.Close()
End If
End Try
Return String.Empty
End Function
有时我需要JToken
的列表,有时我需要一个字符串列表,所以我的两种方法都可以工作。
编译前显示错误,不接受Return RequestServer(methodName, parameters.Select(Function(p) New JValue(p)).ToList())
上面写着:
无法将 List (Of JValue) 转换为 List (Of String)
我在这里做错了什么?
【问题讨论】:
分享完整的错误详情很重要。 @Crowcoder 它说`不可能将 JValue 列表转换为 String 列表` C# 代码正在将List(Of JValue)
转换为 List(of JToken)
,但在 C# 到 VB 的翻译过程中这种转换丢失了。
我不确定JValue
是否继承JToken
,但如果是,请尝试Return RequestServer(methodName, parameters.Select(Function(p) New JValue(p)).Cast(Of JToken)().ToList())
@SeanSkelly 太好了,请把它作为答案,所以我标记它,谢谢
【参考方案1】:
C# 代码将 List(Of JValue)
转换为 List(of JToken)
,但在将 C# 转换为 VB 时丢失了此转换。
如果 JValue 继承了 JToken,那么您可以使用 Enumerable.Cast 将您的可枚举从一种类型转换为另一种类型(在调用 .ToList() 之前):
Public Shared Function RequestServer(ByVal methodName As String, ByVal parameters As List(Of String)) As String
Return RequestServer(methodName, parameters.Select(Function(p) New JValue(p)).Cast(Of JToken).ToList())
End Function
【讨论】:
以上是关于从 C# 转换为 vb.net 后,它显示错误的主要内容,如果未能解决你的问题,请参考以下文章