简体   繁体   English

需要一些帮助将 c# 代码片段转换为 VB.Net

[英]Need some help converting a c# code snippet to VB.Net

I have code snippet written in C# that I need to convert to VB.NET.我有用 C# 编写的代码片段,我需要将其转换为 VB.NET。 I've been struggling with this for hours and hours.我已经为此苦苦挣扎了好几个小时。 Also tried tools like Telerik Converter and Vistual Studio plugin but I always end up with some compile error...sigh... I would really appreciate if someone could help me with this... Thanks in advance... This is the snippet:还尝试了 Telerik 转换器和 Vistual Studio 插件之类的工具,但我总是会遇到一些编译错误...叹息...如果有人可以帮助我,我将不胜感激...提前致谢...这是片段:

hubConnection = new HubConnectionBuilder()
     .WithUrl($"your host/chatHub", (opts) =>
     {
         opts.HttpMessageHandlerFactory = (message) =>
         {
             if (message is HttpClientHandler clientHandler)
                 // bypass SSL certificate```

                 clientHandler.ServerCertificateCustomValidationCallback +=
                     (sender, certificate, chain, sslPolicyErrors) => { return true; };
             return message;
         };
     }).Build();

The callback assignment looks like it could be replaced with an event handler, but from here , it appears you can just assign a lambda to the callback property.回调分配看起来可以用事件处理程序替换,但是从这里,您似乎可以将 lambda 分配给回调属性。

Private Sub oneShot()
    Dim hubConnection = New HubConnectionBuilder().WithUrl(
        $"your host/chatHub",
        Sub(opts)
            opts.HttpMessageHandlerFactory =
            Function(message)
                Dim clientHandler = TryCast(message, HttpClientHandler)
                If clientHandler IsNot Nothing Then
                    clientHandler.ServerCertificateCustomValidationCallback = [Delegate].Combine(
                        clientHandler.ServerCertificateCustomValidationCallback,
                        Function(sender As HttpRequestMessage, certificate As X509Certificate2, chain As X509Chain, sslPolicyErrors As SslPolicyErrors) True)
                End If
                Return message
            End Function
        End Sub).Build()

End Sub

Private Sub brokenUp()
    Dim callback = Function(sender As HttpRequestMessage, certificate As X509Certificate2, chain As X509Chain, sslPolicyErrors As SslPolicyErrors) True
    Dim factory = Function(message As HttpMessageHandler)
                      Dim clientHandler = TryCast(message, HttpClientHandler)
                      If clientHandler IsNot Nothing Then
                            clientHandler.ServerCertificateCustomValidationCallback = [Delegate].Combine(clientHandler.ServerCertificateCustomValidationCallback, callback)
                      End If
                      Return message
                  End Function
    Dim hubConnection = New HubConnectionBuilder().WithUrl($"your host/chatHub", Sub(opts) opts.HttpMessageHandlerFactory = factory).Build()
End Sub

The C# code is not as clear as one would hope, but the VB.NET equivalent should be the following (assuming that the "+=" is wiring up an event handler): C# 代码并不像人们希望的那样清晰,但 VB.NET 等效代码应如下所示(假设“+=”正在连接事件处理程序):

hubConnection = (New HubConnectionBuilder()).WithUrl($"your host/chatHub", Sub(opts)
    opts.HttpMessageHandlerFactory = Function(message)
        If TypeOf message Is HttpClientHandler Then
            Dim clientHandler As HttpClientHandler = CType(message, HttpClientHandler)

            ' bypass SSL certificate```

            AddHandler clientHandler.ServerCertificateCustomValidationCallback, Function(sender, certificate, chain, sslPolicyErrors)
                Return True
            End Function
        End If
        Return message
    End Function
End Sub).Build()

Personally, I would break up the triply-nested lambdas.就个人而言,我会分解三重嵌套的 lambda。

I would first like to thank you all for your efforts to help me get out of the woods.我首先要感谢大家为帮助我走出困境所做的努力。 There are some useful suggestions which I will definitely give a go.有一些有用的建议我一定会给出 go。 For now I have moved the C# code to an existing class library that already contains a number of common functionalities.现在,我已将 C# 代码移动到现有的 class 库中,该库已经包含许多常用功能。 I guess that connecting to a SignalR Hub could be considered as a reusable feature.我猜想连接到 SignalR 集线器可以被视为可重用功能。 Thanks again.再次感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM