如何在 vb.net 中使用 lambda
how to use lambda in vb.net
当我在 VB.NET 中使用 AngleSharp 时,C# 中的 lambda 变成了一个问题。 VB.NET 可以用 function
代替 lambda 但似乎只接受一个参数
Dim document = Await context.OpenAsync(Function(Res)
Res.Content("
")
.Address("http://localhost/")
.Header(HeaderNames.SetCookie, my_headers)
End Function
)
错误:
Error BC30518 Overload resolution failed because no accessible 'OpenAsync' can be called with these arguments:
Extension method 'Public Function OpenAsync(response As IResponse, [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'IResponse' because 'IResponse' is not a delegate type.
Extension method 'Public Function OpenAsync(request As DocumentRequest, [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'DocumentRequest' because 'DocumentRequest' is not a delegate type.
Extension method 'Public Function OpenAsync(url As Url, [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'Url' because 'Url' is not a delegate type.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Leading '.' or '!' can only appear inside a 'With' statement.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Leading '.' or '!' can only appear inside a 'With' statement.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': 'HeaderNames' is not declared. It may be inaccessible due to its protection level.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Leading '.' or '!' can only appear inside a 'With' statement.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Leading '.' or '!' can only appear inside a 'With' statement.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': 'HeaderNames' is not declared. It may be inaccessible due to its protection level.
Extension method 'Public Function OpenAsync(address As String, [cancellation As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'String' because 'String' is not a delegate type.
我该如何解决?
41686d6564修改后的错误
Error BC30518 Overload resolution failed because no accessible 'OpenAsync' can be called with these arguments:
Extension method 'Public Function OpenAsync(response As IResponse, [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'IResponse' because 'IResponse' is not a delegate type.
Extension method 'Public Function OpenAsync(request As DocumentRequest, [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'DocumentRequest' because 'DocumentRequest' is not a delegate type.
Extension method 'Public Function OpenAsync(url As Url, [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'Url' because 'Url' is not a delegate type.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': 'Return' statement in a Sub or a Set cannot return a value.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': 'HeaderNames' is not declared. It may be inaccessible due to its protection level.
Extension method 'Public Function OpenAsync(address As String, [cancellation As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'String' because 'String' is not a delegate type.
您的代码有两个问题:
您尝试使用的 overload 需要一个 Action
,而不是 Func
。因此,您应该使用 Sub
而不是 Function
。
在 VB 中,点必须出现在行尾,而不是下一行的开头(在 C# 中,您可以这样做),否则必须使用 line continuation character(即 _
)。
将您的代码更改为类似以下内容应该可以编译:
Dim document = Await context.OpenAsync(
Sub(Res)
Res.Content("").
Address("http://localhost/").
Header(HeaderNames.SetCookie, my_headers)
End Sub)
或使用明确的行继续:
Dim document = Await context.OpenAsync(
Sub(Res)
Res.Content("") _
.Address("http://localhost/") _
.Header(HeaderNames.SetCookie, my_headers)
End Sub)
当我在 VB.NET 中使用 AngleSharp 时,C# 中的 lambda 变成了一个问题。 VB.NET 可以用 function
代替 lambda 但似乎只接受一个参数
Dim document = Await context.OpenAsync(Function(Res)
Res.Content("
")
.Address("http://localhost/")
.Header(HeaderNames.SetCookie, my_headers)
End Function
)
错误:
Error BC30518 Overload resolution failed because no accessible 'OpenAsync' can be called with these arguments:
Extension method 'Public Function OpenAsync(response As IResponse, [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'IResponse' because 'IResponse' is not a delegate type.
Extension method 'Public Function OpenAsync(request As DocumentRequest, [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'DocumentRequest' because 'DocumentRequest' is not a delegate type.
Extension method 'Public Function OpenAsync(url As Url, [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'Url' because 'Url' is not a delegate type.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Leading '.' or '!' can only appear inside a 'With' statement.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Leading '.' or '!' can only appear inside a 'With' statement.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': 'HeaderNames' is not declared. It may be inaccessible due to its protection level.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Leading '.' or '!' can only appear inside a 'With' statement.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Leading '.' or '!' can only appear inside a 'With' statement.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': 'HeaderNames' is not declared. It may be inaccessible due to its protection level.
Extension method 'Public Function OpenAsync(address As String, [cancellation As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'String' because 'String' is not a delegate type.
我该如何解决?
41686d6564修改后的错误
Error BC30518 Overload resolution failed because no accessible 'OpenAsync' can be called with these arguments:
Extension method 'Public Function OpenAsync(response As IResponse, [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'IResponse' because 'IResponse' is not a delegate type.
Extension method 'Public Function OpenAsync(request As DocumentRequest, [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'DocumentRequest' because 'DocumentRequest' is not a delegate type.
Extension method 'Public Function OpenAsync(url As Url, [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'Url' because 'Url' is not a delegate type.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': 'Return' statement in a Sub or a Set cannot return a value.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': 'HeaderNames' is not declared. It may be inaccessible due to its protection level.
Extension method 'Public Function OpenAsync(address As String, [cancellation As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'String' because 'String' is not a delegate type.
您的代码有两个问题:
您尝试使用的 overload 需要一个
Action
,而不是Func
。因此,您应该使用Sub
而不是Function
。在 VB 中,点必须出现在行尾,而不是下一行的开头(在 C# 中,您可以这样做),否则必须使用 line continuation character(即
_
)。
将您的代码更改为类似以下内容应该可以编译:
Dim document = Await context.OpenAsync(
Sub(Res)
Res.Content("").
Address("http://localhost/").
Header(HeaderNames.SetCookie, my_headers)
End Sub)
或使用明确的行继续:
Dim document = Await context.OpenAsync(
Sub(Res)
Res.Content("") _
.Address("http://localhost/") _
.Header(HeaderNames.SetCookie, my_headers)
End Sub)