You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET使用AngleSharp时OpenAsync方法Lambda表达式报错如何解决?

错误原因

  • 委托类型不匹配:OpenAsync方法需要传入的是无返回值的Action(Of VirtualResponse)委托,VB.NET中无返回值的Lambda需使用Sub关键字声明,你之前使用的Function仅用于声明带返回值的Func系列委托,类型不匹配导致重载解析失败。
  • 链式调用语法不符合VB规范:VB不支持C#中直接换行以.开头的链式调用写法,要么在每行末尾加行继续符_,要么使用With块包裹成员调用。
  • 命名空间未导入:HeaderNames类属于AngleSharp.Net命名空间,未导入会提示未声明。

修复步骤

  1. 先导入必要的命名空间:
Imports AngleSharp
Imports AngleSharp.Net
Imports System.Threading.Tasks
  1. 替换Lambda为正确的Sub写法,以下两种写法二选一即可:

写法1:使用行继续符实现链式调用

Dim document = Await context.OpenAsync(Sub(res)
    res.Content("") _
       .Address("http://localhost/") _
       .Header(HeaderNames.SetCookie, my_headers)
End Sub)

写法2:使用With块简化调用

Dim document = Await context.OpenAsync(Sub(res)
    With res
        .Content("")
        .Address("http://localhost/")
        .Header(HeaderNames.SetCookie, my_headers)
    End With
End Sub)

内容的提问来源于stack exchange,提问作者邱子恩

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 05:00:04