C# 在 VB.NET 中使用 With 等价物

C# Using With Equivalent in VB.NET

var baseAddress = new Uri("http://www.aaa.com");
    var cookieContainer = new CookieContainer();
    using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
    using (var client = new HttpClient(handler){ BaseAddress = baseAddress })
{}

我尝试将带有 Developer Fusion tool 的代码转换为 VB.NET,但没有成功。

Dim baseAddress = New Uri("http://www.aaa.com")
Dim cookieContainer = New CookieContainer()
Using handler = New HttpClientHandler() With { _
    Key .CookieContainer = cookieContainer _
}
    Using client = New HttpClient(handler) With { _
        Key .BaseAddress = baseAddress _
    }
    End Using
End Using

发生错误"key . "

此代码的 VB.NET 等价物是什么(使用 with 语句)?

只需删除 Key 字词

Using handler = New HttpClientHandler() With { _
    .CookieContainer = cookieContainer _
}
    Using client = New HttpClient(handler) With { _
        .BaseAddress = baseAddress _
    }
    End Using
End Using

我从 Kilanny 的回答中学到了一些新东西 (Object Initializers: Named and Anonymous Types);这是我如何重构转换后的代码:

    Dim baseAddress = New Uri("http://www.aaa.com")
    Dim cookieContainer = New Net.CookieContainer()
    Using handler As New HttpClientHandler
        With handler
            .CookieContainer = cookieContainer
            Using client As New HttpClient(handler)
                With client
                    .BaseAddress = baseAddress
                End With
            End Using
        End With
    End Using
  1. Object Initializers: Named and Anonymous Types (Visual Basic)
  2. Using Statement (Visual Basic)
  3. With...End With Statement (Visual Basic)