翻译代码片段从c#到VB

本文关键字:VB 代码 片段 翻译 | 更新日期: 2023-09-27 18:09:56

我需要将下面代码片段中的第4行翻译成VB。因为某些原因,我今晚无法完成。我要么太累了,要么人才流失了……你能帮忙吗?

var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier = ClientCredentials.ClientID;
provider.ClientSecret = ClientCredentials.ClientSecret;
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthentication);

翻译工具让我走了这么远,但这是不对的。

Dim provider As NativeApplicationClient = New NativeApplicationClient(GoogleAuthenticationServer.Description)
provider.ClientIdentifier = ClientCredentials.ClientID
provider.ClientSecret = ClientCredentials.ClientSecret
Dim auth As OAuth2Authenticator(Of NativeApplicationClient) = New OAuth2Authenticator(Of NativeApplicationClient)(provider, GetAuthorization)

GetAuthorization方法有如下签名:

Private Function GetAuthorization(ByVal arg As NativeApplicationClient) As IAuthorizationState

翻译代码片段从c#到VB

您需要AddressOf

Dim auth As OAuth2Authenticator(Of NativeApplicationClient) = New OAuth2Authenticator(Of NativeApplicationClient)(provider, AddressOf GetAuthorization)

请参阅本网站以获取从c#到VB的任何代码的翻译转换后的代码为

Dim provider = New NativeApplicationClient(GoogleAuthenticationServer.Description)
provider.ClientIdentifier = ClientCredentials.ClientID
provider.ClientSecret = ClientCredentials.ClientSecret
Dim auth = New OAuth2Authenticator(Of NativeApplicationClient)(provider, GetAuthentication)

您正在尝试使用OAuth2Authenticator来GetAuthorization,两者是不同的,也许这是您的问题。

AddressOf永远不会调用该函数,您需要使用

Dim getAuth As Func(Of NativeApplicationClient, IAuthorizationState) = AddressOf GetAuthorization
Dim auth As New OAuth2Authenticator(Of NativeApplicationClient)(provider, getAuth)