473,545 Members | 1,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to identify the calling method of a method

Hi,

I have a method Show() which is invoked by several
methods at different intervals. I want to identify the
calling method within Show() whenever it is invoked. How
do i do this?

Thanks in advance
Rakesh

Nov 15 '05 #1
8 6616
Lookup StackTrace and StackFrame classes in System.Diagnost ics namespace.

vJ

"Rakehs" <as*@asd.com> wrote in message
news:03******** *************** *****@phx.gbl.. .
Hi,

I have a method Show() which is invoked by several
methods at different intervals. I want to identify the
calling method within Show() whenever it is invoked. How
do i do this?

Thanks in advance
Rakesh

Nov 15 '05 #2
Hi Rakesh,

Actually, you can't for sure.
The only way would be inspecting the stack as Vijaye noted.
However, it takes a lot of time and it isn't reliable because of code
optimizations.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

"Rakehs" <as*@asd.com> wrote in message
news:03******** *************** *****@phx.gbl.. .
Hi,

I have a method Show() which is invoked by several
methods at different intervals. I want to identify the
calling method within Show() whenever it is invoked. How
do i do this?

Thanks in advance
Rakesh

Nov 15 '05 #3
THanks! Thanks a lot!

-----Original Message-----
Lookup StackTrace and StackFrame classes in System.Diagnost ics namespace.
vJ

"Rakehs" <as*@asd.com> wrote in message
news:03******* *************** ******@phx.gbl. ..
Hi,

I have a method Show() which is invoked by several
methods at different intervals. I want to identify the
calling method within Show() whenever it is invoked. How do i do this?

Thanks in advance
Rakesh

.

Nov 15 '05 #4
But when I use the StackFrame.GetM ethod().Name property,
this returns the method i am in and not the calling
method...
-----Original Message-----
Lookup StackTrace and StackFrame classes in System.Diagnost ics namespace.
vJ

"Rakehs" <as*@asd.com> wrote in message
news:03******* *************** ******@phx.gbl. ..
Hi,

I have a method Show() which is invoked by several
methods at different intervals. I want to identify the
calling method within Show() whenever it is invoked. How do i do this?

Thanks in advance
Rakesh

.

Nov 15 '05 #5
As Miha pointed out, there may be code optimizations that would give you
different results. Some functions could be completely optimized out and may
never appear in a call stack. But the following code should work fine on a
Debug build (with no optimizations)

This is how you get the full stack trace. (Note: The following code is
taken from MSDN - have not tried it myself.)

// Create a StackTrace that captures
// filename, line number and column
// information, for the current thread.
StackTrace st = new StackTrace(true );
for(int i =0; i< st.FrameCount; i++ )
{
// High up the call stack, there is only one stack frame
StackFrame sf = st.GetFrame(i);
Console.WriteLi ne("\nHigh Up the Call Stack, Method: {0}",
sf.GetMethod() );
}
"Rakesh" <an*******@disc ussions.microso ft.com> wrote in message
news:01******** *************** *****@phx.gbl.. .
But when I use the StackFrame.GetM ethod().Name property,
this returns the method i am in and not the calling
method...
-----Original Message-----
Lookup StackTrace and StackFrame classes in

System.Diagnost ics namespace.

vJ

"Rakehs" <as*@asd.com> wrote in message
news:03******* *************** ******@phx.gbl. ..
Hi,

I have a method Show() which is invoked by several
methods at different intervals. I want to identify the
calling method within Show() whenever it is invoked. How do i do this?

Thanks in advance
Rakesh

.

Nov 15 '05 #6
Isnt there another way to get the caller via reflection? If not, should
there be?
"Vijaye Raji" <no************ @hotmail.com> wrote in message
news:1N******** ************@co mcast.com...
As Miha pointed out, there may be code optimizations that would give you
different results. Some functions could be completely optimized out and may never appear in a call stack. But the following code should work fine on a Debug build (with no optimizations)

This is how you get the full stack trace. (Note: The following code is
taken from MSDN - have not tried it myself.)

// Create a StackTrace that captures
// filename, line number and column
// information, for the current thread.
StackTrace st = new StackTrace(true );
for(int i =0; i< st.FrameCount; i++ )
{
// High up the call stack, there is only one stack frame
StackFrame sf = st.GetFrame(i);
Console.WriteLi ne("\nHigh Up the Call Stack, Method: {0}",
sf.GetMethod() );
}
"Rakesh" <an*******@disc ussions.microso ft.com> wrote in message
news:01******** *************** *****@phx.gbl.. .
But when I use the StackFrame.GetM ethod().Name property,
this returns the method i am in and not the calling
method...
-----Original Message-----
Lookup StackTrace and StackFrame classes in

System.Diagnost ics namespace.

vJ

"Rakehs" <as*@asd.com> wrote in message
news:03******* *************** ******@phx.gbl. ..
> Hi,
>
> I have a method Show() which is invoked by several
> methods at different intervals. I want to identify the
> calling method within Show() whenever it is invoked.

How
> do i do this?
>
> Thanks in advance
> Rakesh
>
.


Nov 15 '05 #7
Hi Alvin,

No, AFAIK there isn't other way than inspecting the stack.
Maybe there is a better approach to your problem (or Rakesh's).

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

"Alvin Bruney" <alvin.bruney@. telia..com.> wrote in message
news:%2******** **********@TK2M SFTNGP11.phx.gb l...
Isnt there another way to get the caller via reflection? If not, should
there be?

Nov 15 '05 #8
Hi Rakehs

Can you tell us why you need to do this? It goes against the normal OO
principle that a class method should never know about its caller -
especially if the caller is a descendent.

Like any rule, there are exceptions. Maybe if you could expand on your
requirements the NG could come up with a more elegant solution.

Regards

Ron

"Rakehs" <as*@asd.com> wrote in message
news:03******** *************** *****@phx.gbl.. .
Hi,

I have a method Show() which is invoked by several
methods at different intervals. I want to identify the
calling method within Show() whenever it is invoked. How
do i do this?

Thanks in advance
Rakesh

Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
15346
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have the same class/static method definition statictly linked to my EXE and when I call InvokeMember(...), even though I got the Type from the new AppDomain,...
4
2022
by: Nikhil | last post by:
I have a dll which is call by an ASP page, this dll has to perform read/write operations on a file, I seem to be encountering the following error" Process cannot access xyz.txt cause its held bby another process", is there any way I can check if the file is being accessed by another process & if so I could wait till that operation is done. If...
5
3399
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to...
12
1831
by: clintonG | last post by:
I can't tell you how frustrated I get when going to a web developer's website and observing he or she is an idiot that has not grasped the most fundamental element of usability: page title naming conventions. 1.) You know you are at an idiot's website when there is no page title. Listen up idiot. Give every page a name using the HTML <title>...
4
6082
by: Goh | last post by:
Hi, I would like to know how can we implement a web page that intelligent enough to unique identify that pc have been visit before without any cookies and login user require. I have try implement this by MAC address. When user browser the web site I sometime can get user pc MAC and sometime no. Why this type of implementation are so...
4
1573
by: Kenneth McDonald | last post by:
I'm trying to write a 'flatten' generator which, when give a generator/iterator that can yield iterators, generators, and other data types, will 'flatten' everything so that it in turns yields stuff by simply yielding the instances of other types, and recursively yields the stuff yielded by the gen/iter objects. To do this, I need to...
6
2483
by: Pieter | last post by:
Hi, For some procedures that throws exceptions, I would like to show different messages to the user depending on what type of exception he's getting. For instance this one: when the file is locked, I want a messagebox to tell that the user has to close the file first. Is there a way to identify an exception by some kind of unique number...
6
12784
by: Ryan Liu | last post by:
Hi, If I want to uniquely identify a computer. I can read CPU ID or Mac Address. I heard, but is this true: some BIOS can block CPU ID from being read? (In this case, will I get an exception, null or empty string for method managementObject.Properties?) So maybe Mac address is better way. But when I read Mac address for my laptop, I...
7
2667
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file name based on the name of the VB6 application. A second choice would be a file name based on the # COM interface assembly. I have tried calling...
0
7416
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7676
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7932
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7442
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6001
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4965
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1905
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
729
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.