C#在VB.NET中使用等效

时间:2015-06-29 08:32:03

标签: c# vb.net code-translation

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

发生错误“密钥。”

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

2 个答案:

答案 0 :(得分:3)

只需删除Key

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

答案 1 :(得分:2)

我从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)