开发者

Creating delegate to pass function with parameters (.net 2.0)

开发者 https://www.devze.com 2023-01-14 20:43 出处:网络
Forgive me if this is documented somewhere, but is there any nice way to create a MethodInvoker which will perform a specified function with specified arguments (capturing the argument values when the

Forgive me if this is documented somewhere, but is there any nice way to create a MethodInvoker which will perform a specified function with specified arguments (capturing the argument values when the MethodInvoker is created)? I've sometimes done things by using a wrapper class, but wonder if that's the best approach. Should I be doing something like:

Class Invoker(Of T)
    ReadOnly Param1 As T
    ReadOnly Act As Action(Of T)
    ReadOnly execDelegate As New MethodInvoker(AddressOf execProc)
    Private Sub New(ByVal param1 As T, ByVal act As Action(Of T))
        Me.Param1 = param1
        Me.Act = act
    End Sub
    Private Sub execProc()
        Act(Param1)
    End Sub
    Shared开发者_如何学Python Function Create(ByVal param1 As T, ByVal act As Action(Of T)) As MethodInvoker
        Dim newAction As New Invoker(Of T)(param1, act)
        Return newAction.execDelegate
    End Function
End Class

and using e.g. "myMethodInvoker = Invoker(Of Integer).Create(6, AddressOf MyRoutine)" to create a delegate which will, when invoked, call MyRoutine(6)? Or is there some built-in support for doing that?

0

精彩评论

暂无评论...
验证码 换一张
取 消