我有
Function Bar(s As String) ...
我需要创建一个函数
Me.Foo(myInt, AddressOf Bar)
我应该如何写 Foo 的签名?
使用通用 Func(Of Type) 关键字可能是最简单的。
Public Function Foo(i As Integer, f As Func(Of String, Integer)) As String
Dim i2 = f.Invoke("test")
Return "42"
End Function
声明您的代表签名:
Public Delegate Sub Format(ByVal value As String)
定义您的测试功能:
Public Sub CheckDifference(ByVal A As Integer, _
ByVal B As Integer, _
ByVal format As Format)
If (B - A) > 5 Then
format.Invoke(String.Format( _
"Difference ({0}) is outside of acceptable range.", (B - A)))
End If
End Sub
在您的代码中的某处调用您的 Test 函数:
CheckDifference(Foo, Bar, AddressOf log.WriteWarn)
或者
CheckDifference(Foo, Bar, AddressOf log.WriteError)