Automatically call all functions matching a certain pattern in VB.NET
I have the same intention as in this question:
Automatically call all functions matching a certain pattern in python
But I need开发者_开发问答ed the solution (if technically possible) in VB.NET. Is that possible in VB.NET ?
Sure you can do that, that's what reflection is for:
Imports System.Reflection
Module Module1
Sub Main()
Dim methods = GetType(Module1).GetMethods() _
.Where(Function(m) m.Name.StartsWith("Setup"))
For Each method As MethodInfo In methods
method.Invoke(Nothing, Nothing)
Next
End Sub
Sub Setup1()
Console.WriteLine(1)
End Sub
Sub Setup2()
Console.WriteLine(2)
End Sub
Sub Setup3()
Console.WriteLine(3)
End Sub
End Module
精彩评论