How to get Class and Procedure name?

How can I get Class, procedure that is inside the class, function, property... signature (name, their parameters, return type)?

Public Class MyClass

    Dim ClassName As String = Here some CRL function to return string MyClass as it is my Class

    Public Sub MySub()
        Dim ProcName As String = Here some CRL function to return string MySub as it is my procedure
        ' Some processing
    End Sub

End Class

Same thing applies with Property, Function, or any other objects.

Thanks.
LVL 5
KarcOriginAsked:
Who is Participating?
 
GlomConnect With a Mentor Commented:
Hi,

The thing you are talking about is called Reflection, let's have a look at the System.Reflection Namespace !

For example for your Sub :

Dim MethodName As String = System.Reflection.MethodInfo.GetCurrentMethod.Name

Glom
0
 
GlomCommented:
And for your class, you cannot initialize classname in its declaration section.
Use the constructor instead

Public ClassName As String

Public Sub New()
    ClassName = Me.GetType().ToString()
End Sub
0
 
KarcOriginAuthor Commented:
It works thanks a lot but can something like below can be done?

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        MsgBox(GetCurrentMethodName()) <-- This should return "btnStart_Click"
        MySub()
    End Sub

    Private Sub MySub()
        MsgBox(GetCurrentMethodName()) <-- This should return "MySub"
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MsgBox(GetCurrentMethodName()) <-- This should return "Form1_Load"


    End Sub

    Private Function GetCurrentMethodName() As String
        ** Use something to return the name of the method or sub from where it is been called
    End Function

End Class

Thanks
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
GlomCommented:
Unfortunately, you cannot know which method called the GetCurrentMethodName method.
I think in this case you must use each time you want to know your method name the following :

Imports System.Reflection

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        MsgBox(MethodInfo.GetCurrentMethod.Name)
        MySub()
    End Sub
......
End Class

This is not much longer and gives the same result.
Hope it helps

Glom
0
 
Ignacio Soler GarciaSolution Architech & Technical LeadCommented:
You can do it in this way. It's excelent that I found the way to do this to get the points but I didn't when I needed this ... humans are very strange .... :^?

    Private Function GetCurrentMethodName() As String
        Dim sf As New StackFrame(2, True)
        Dim st As New StackTrace(sf)

        return st.toString()
    End Function

Maybe you will need some string formatting to let the string like you want ....

Hope this help. ask for more if you need ...

SoMoS
0
 
Ignacio Soler GarciaSolution Architech & Technical LeadCommented:
Or this other that gives you the string better formatted:

    Private Function GetCurrentMethodName() As String
        Dim strace As New StackTrace
        Dim frame As New StackFrame
        frame = strace.GetFrame(1)

        return frame.GetMethod().Name()
    End Function

0
 
Ignacio Soler GarciaConnect With a Mentor Solution Architech & Technical LeadCommented:
And finally, the shortest one :)

    Private Function GetCurrentMethodName() As String
        return New StackTrace().GetFrame(1).GetMethod.Name
    End Function

It's always best to think first and write after this but It's hard for me ... :D
0
 
GlomCommented:
Wow, good job SoMoS !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.