How to obtain method name of caller

  • Thread starter Thread starter OneFang
  • Start date Start date
O

OneFang

Hi

I hope I make sense here.

I want to be able to obtain the name of the method that calls a method
within my class.

So If I have my class that has a method LogInfo()

And my client code has a method ProcessRequest()
that makes a call to myClass.LogInfo(data)

then I want to be able to obtain the name 'ProcessRequest' from within
the LogInfo() method, without having to pass the method name across.

I'm assuming it's some form of reflection, but really just guessing.

thanks for any info.

Martyn
 
Hi,

Try this:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Call f()
End Sub

Sub f()
Dim o As New StackTrace()
MsgBox(o.GetFrame(1).GetMethod.Name)
End Sub

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
Unfortunately code like this breaks when run on an assembly that has been
obfuscated.

:(

I have not found a way around that yet... I used to use code like this in my
debug statements until I found that out.
 
Yes, that's the job of an obfuscator...

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
I know, just pointing out the fact that it becomes harder to use these
automatic methods of getting caller name for debug purposes when using an
obfuscator. It was something that my original code had not taken into
account and I wanted to save them the time of finding that out latter.

Not sure why I thought it would be different but the fact that the metadata
in the assembly gets munged by obfuscation did not seem to enter into my
mind..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top