By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,572 Members | 1,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,572 IT Pros & Developers. It's quick & easy.

How to obtain method name of caller

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
"OneFang" <ma***********@hotmail.com> escribió en el mensaje
news:11**********************@g44g2000cwa.googlegr oups.com...
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

Nov 21 '05 #2

P: n/a
many thanks Carlos; that did it.

M

Nov 21 '05 #3

P: n/a
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.

"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:e6**************@TK2MSFTNGP12.phx.gbl...
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
"OneFang" <ma***********@hotmail.com> escribió en el mensaje
news:11**********************@g44g2000cwa.googlegr oups.com...
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


Nov 21 '05 #4

P: n/a
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

"Ray Cassick" <rc******@nospam.enterprocity.com> escribió en el mensaje
news:ei**************@TK2MSFTNGP10.phx.gbl...
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.


Nov 21 '05 #5

P: n/a
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..

"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:uT*************@TK2MSFTNGP10.phx.gbl...
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

"Ray Cassick" <rc******@nospam.enterprocity.com> escribió en el mensaje
news:ei**************@TK2MSFTNGP10.phx.gbl...
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.

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.