开发者

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
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜