如果您想将可变数量的参数传递给函数,我使用了数组。你不必
Dim methods As New Dictionary(Of String, Func(Of Double(), Double))
Dim results as double
methods.Add("Add", Function(values() As Double) values(0) + values(1))
methods.Add("Sub", Function(values() As Double) values(0) - values(1))
methods.Add("Mul", Function(values() As Double) values(0) * values(1))
methods.Add("Div", Function(values() As Double) values(0) / values(1))
results = methods("Add")(New Double() {1, 2})
results = methods("Sub")(New Double() {5, 3})
results = methods("Mul")(New Double() {4, 8})
results = methods("Div")(New Double() {1, 2})
没有数组...
Dim methods As New Dictionary(Of String, Func(Of Double, Double, Double))
methods.Add("Add", Function(a As Double, b As Double)
Return a + b
End Function)
methods.Add("Sub", Function(a As Double, b As Double)
Return a - b
End Function)
methods.Add("Mul", Function(a As Double, b As Double)
Return a * b
End Function)
methods.Add("Div", Function(a As Double, b As Double)
Return a / b
End Function)
results = methods("Add")(1, 2)
results = methods("Sub")(5, 3)
results = methods("Mul")(4, 8)
results = methods("Div")(1, 2)