473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to obtain method name of caller

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
5 2048
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
many thanks Carlos; that did it.

M

Nov 21 '05 #3
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alex Vinokur | last post by:
========================================= Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927 (prerelease) ========================================= Here is some program...
4
by: Gnanaprakash Rathinam | last post by:
Hi Expert, Is there a way to obtain assembly name in an unmanaged call? During Interop call between managed to unmanaged, I would like to know in unmanaged code about the caller of assembly file...
9
by: jaden10001 | last post by:
I have read that the function.caller method is now depricated. I see alot of browsers still support it but Opera 7 does not. Is there a way to get the name of the caller of a function in Opera 7?
20
by: 2pac | last post by:
in this scenario foo1 ---calls---> foo2 is it possible for me to print out - when the control is within foo2 - the caller of foo2 the information is obviously there in the stack - am wondering...
9
by: Steve | last post by:
Hello -- I've been struggling with this problem for over a day now. I'd like to know (without passing parameters) which class, and preferably, which method of that class has called my function....
26
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
5
by: pamelafluente | last post by:
Hi guys, How do I get the full name of the current (overload) function or sub (whatever it be) ? Sub SomeFunction() Dim FullNameOfThisFunction = ??? msgbox(FullNameOfThisFunction )
2
by: ME | last post by:
How would one obtain the parameter VALUES of a method that has already run? I can find the method using the StackTrace and StackFrame classes but once I find the method I would like to obtain the...
7
by: jackchang1 | last post by:
It's not difficult getting the function name of the caller but if the caller is an object's method, how do you get the method name? <html> <head> <script type="text/javascript"> function...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.