开发者

Automatically call all functions matching a certain pattern in VB.NET

开发者 https://www.devze.com 2023-02-28 22:15 出处:网络
I have the same intention as in this question: Automatically call all functions matching a certain pattern in python

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

精彩评论

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

关注公众号