尝试将C#函数转换为等效的VB.Net时出现编译错误。 C#中的PageAsyncTask需要一个Task输入,但是在VB.Net中它正在寻找Func(Of Task)。我找不到的在线转换器正确翻译了该语言。
错误是:
类型“任务”的值不能转换为“功能(任务)”

不确定如何进行(我想我需要定义一个事件?)。
这是原始的C#代码

        protected void Page_Load(object sender, EventArgs e)
    {
        {
            AsyncMode = true;
            if (!dictionary.ContainsKey("accessToken"))
            {
                if (Request.QueryString.Count > 0)
                {
                    var response = new AuthorizeResponse(Request.QueryString.ToString());
                    if (response.State != null)
                    {
                        if (oauthClient.CSRFToken == response.State)
                        {
                            if (response.RealmId != null)
                            {
                                if (!dictionary.ContainsKey("realmId"))
                                {
                                    dictionary.Add("realmId", response.RealmId);
                                }
                            }

                            if (response.Code != null)
                            {
                                authCode = response.Code;
                                output("Authorization code obtained.");
                                PageAsyncTask t = new PageAsyncTask(performCodeExchange);
                                Page.RegisterAsyncTask(t);
                                Page.ExecuteRegisteredAsyncTasks();
                            }
                        }
                        else
                        {
                            output("Invalid State");
                            dictionary.Clear();
                        }
                    }
                }
            }
            else
            {
                homeButtons.Visible = false;
                connected.Visible = true;
            }
        }
    }


并将代码转换为:

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If True Then
            AsyncMode = True
            If Not dictionary.ContainsKey("accessToken") Then
                If Request.QueryString.Count > 0 Then
                    Dim response = New AuthorizeResponse(Request.QueryString.ToString())
                    If response.State IsNot Nothing Then
                        If oauthClient.CSRFToken = response.State Then
                            If response.RealmId IsNot Nothing Then
                                If Not dictionary.ContainsKey("realmId") Then
                                    dictionary.Add("realmId", response.RealmId)
                                End If
                            End If

                            If response.Code IsNot Nothing Then
                                authCode = response.Code
                                output("Authorization code obtained.")
                                Dim t As New PageAsyncTask(performCodeExchange)
                                Page.RegisterAsyncTask(t)
                                Page.ExecuteRegisteredAsyncTasks()
                            End If
                        Else
                            output("Invalid State")
                            dictionary.Clear()
                        End If
                    End If
                End If
            Else
                homeButtons.Visible = False
                connected.Visible = True
            End If
        End If
    End Sub


问题领域:

Dim t As New PageAsyncTask(performCodeExchange)


任务函数是performCodeExchange,它返回一个Task

    Public Async Function performCodeExchange() As Task
    output("Exchanging code for tokens.")
    Try
        Dim tokenResp = Await oauthClient.GetBearerTokenAsync(authCode)
        If Not _dictionary.ContainsKey("accessToken") Then
            _dictionary.Add("accessToken", tokenResp.AccessToken)
        Else
            _dictionary("accessToken") = tokenResp.AccessToken
        End If

        If Not _dictionary.ContainsKey("refreshToken") Then
            _dictionary.Add("refreshToken", tokenResp.RefreshToken)
        Else
            _dictionary("refreshToken") = tokenResp.RefreshToken
        End If

        If tokenResp.IdentityToken IsNot Nothing Then
            idToken = tokenResp.IdentityToken
        End If
        If Request.Url.Query = "" Then
            Response.Redirect(Request.RawUrl)
        Else
            Response.Redirect(Request.RawUrl.Replace(Request.Url.Query, ""))
        End If
    Catch ex As Exception
        output("Problem while getting bearer tokens.")
    End Try
End Function


为了更全面,请使用原始的C#代码:

       public async Task performCodeExchange()
    {
        output("Exchanging code for tokens.");
        try
        {
            var tokenResp = await oauthClient.GetBearerTokenAsync(authCode);
            if (!dictionary.ContainsKey("accessToken"))
                dictionary.Add("accessToken", tokenResp.AccessToken);
            else
                dictionary["accessToken"] = tokenResp.AccessToken;

            if (!dictionary.ContainsKey("refreshToken"))
                dictionary.Add("refreshToken", tokenResp.RefreshToken);
            else
                dictionary["refreshToken"] = tokenResp.RefreshToken;

            if (tokenResp.IdentityToken != null)
                idToken = tokenResp.IdentityToken;
            if (Request.Url.Query == "")
            {
                Response.Redirect(Request.RawUrl);
            }
            else
            {
                Response.Redirect(Request.RawUrl.Replace(Request.Url.Query, ""));
            }
        }
        catch (Exception ex)
        {
            output("Problem while getting bearer tokens.");
        }
    }


我不确定在这里做什么-传递一个代表?如何通过任务来完成(在VB.Net中)?

最佳答案

在C#中执行PageAsyncTask t = new PageAsyncTask(performCodeExchange);时,将隐式创建一个指向performCodeExchange方法的委托并将其传递给PageAsyncTask的构造函数。

现在,VB中的语句Dim t As New PageAsyncTask(performCodeExchange)有所不同。 VB中的函数可以不带括号地求值,因此等效于Dim t As New PageAsyncTask(performCodeExchange())。这意味着PageAsyncTask的构造函数将接收performCodeExchange的求值结果,而不是方法的委托。

要在VB中获得委托,可以使用AdressOf关键字。该代码应重写为:

Dim t As New PageAsyncTask(AddressOf performCodeExchange)

10-08 06:16