VB.NET 函数将其他函数作为参数并执行它们

发布于 2024-08-30 15:59:47 字数 308 浏览 8 评论 0原文

vb.net中有没有一种方法可以创建一个子/函数,它将某种指向另一个函数的指针作为参数,并允许这个新的子/函数执行传递的函数?

我拥有的是针对远程服务器调用的 10-12 个 xml-rpc 函数。这些函数中的每一个都有不同的参数列表(一个需要 1 个字符串,另一个可能需要 3 个字符串和一个整数,等等)。它们都返回一个对象。

正如我所说的那样,似乎应该能够更好地分解它。例如,每次调用这些函数中的任何一个时,我都想测试会话丢弃的返回值,并执行一些操作来尝试重新连接到远程系统等。

使用 .net 3.5

谢谢!

-R

Is there a way in vb.net to create a sub/function that will take as an argument some kind of pointer to another function, and allow this new sub/function to execute the passed function?

What I have are 10-12 xml-rpc functions I am calling against a remote server. Each of these functions has different argument lists (one takes 1 string, another might take 3 strings and one int, etc). All of them return an object.

As I am calling these, it seems like it should be able to be factored better. For instance, everytime I call any of these functions, I want to test the return value for a session drop, and do something to try and reconnect to the remote system, etc.

Using .net 3.5

Thanks!

-R

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终止放荡 2024-09-06 15:59:47

您需要...Func< /a>'y 镇

You need to be taken...to Func'y town

如痴如狂 2024-09-06 15:59:47
Public Sub DoSomething(outerFunction as Func(of T))
    ' do something

    ' call passed in function
    Dim value = outerFunction
End Sub
Public Sub DoSomething(outerFunction as Func(of T))
    ' do something

    ' call passed in function
    Dim value = outerFunction
End Sub
幸福丶如此 2024-09-06 15:59:47

经过更多研究,我想出了一个解决方案:

使用 CallByName 函数:

MSDN参考

这使我能够拥有一个实际运行 12 个单独函数的函数,并使我能够拥有一个集中式错误处理系统:

   Private Function RunRemoteRequest(ByVal functionName As String, ByVal service_url As String, ByVal args() As Object) As Object
    Dim retnVal As Object

    Dim success As Boolean = False
    While success = False And Me._connAttemptCount < MAX_ATTEMPTS
        Try
            retnVal = CallByName(remProxy, functionName, Method, args)
            success = True
            Me._connAttemptCount = 0
        Catch ex As Exception
            Me._connAttemptCount += 1
            If ex.Message = "Error I am looking for" Then
                Me.Login()
            Else
                log.Error("Error in RunRemoteRequest(" & functionName & ").", ex)
            End If
        End Try
    End While

    RunRemoteRequest = retnVal

End Function 

请注意,您需要在您所在的模块/类中导入 Microsoft.VisualBasic.CallType正在努力。

After some more research, I came up with a solution:

Using the CallByName function:

MSDN reference

This allowed me to have one function that actually ran the 12 individual functions, and enabled me to have a centralized error handling system:

   Private Function RunRemoteRequest(ByVal functionName As String, ByVal service_url As String, ByVal args() As Object) As Object
    Dim retnVal As Object

    Dim success As Boolean = False
    While success = False And Me._connAttemptCount < MAX_ATTEMPTS
        Try
            retnVal = CallByName(remProxy, functionName, Method, args)
            success = True
            Me._connAttemptCount = 0
        Catch ex As Exception
            Me._connAttemptCount += 1
            If ex.Message = "Error I am looking for" Then
                Me.Login()
            Else
                log.Error("Error in RunRemoteRequest(" & functionName & ").", ex)
            End If
        End Try
    End While

    RunRemoteRequest = retnVal

End Function 

Note that you need to have Imports Microsoft.VisualBasic.CallType in the module/class you are working on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文