473,386 Members | 1,785 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,386 software developers and data experts.

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 6608
Lookup StackTrace and StackFrame classes in System.Diagnostics 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.Diagnostics 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.GetMethod().Name property,
this returns the method i am in and not the calling
method...
-----Original Message-----
Lookup StackTrace and StackFrame classes in System.Diagnostics 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.WriteLine("\nHigh Up the Call Stack, Method: {0}",
sf.GetMethod() );
}
"Rakesh" <an*******@discussions.microsoft.com> wrote in message
news:01****************************@phx.gbl...
But when I use the StackFrame.GetMethod().Name property,
this returns the method i am in and not the calling
method...
-----Original Message-----
Lookup StackTrace and StackFrame classes in

System.Diagnostics 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********************@comcast.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.WriteLine("\nHigh Up the Call Stack, Method: {0}",
sf.GetMethod() );
}
"Rakesh" <an*******@discussions.microsoft.com> wrote in message
news:01****************************@phx.gbl...
But when I use the StackFrame.GetMethod().Name property,
this returns the method i am in and not the calling
method...
-----Original Message-----
Lookup StackTrace and StackFrame classes in

System.Diagnostics 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******************@TK2MSFTNGP11.phx.gbl...
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
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...
4
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...
5
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...
12
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...
4
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...
4
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...
6
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...
6
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,...
7
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.