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

How to identify the calling method of a method

P: n/a
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
Share this Question
Share on Google+
8 Replies


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

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

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

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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.