468,733 Members | 2,182 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,733 developers. It's quick & easy.

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 6372
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Nick Flandry | last post: by
12 posts views Thread by clintonG | last post: by
4 posts views Thread by Kenneth McDonald | last post: by
6 posts views Thread by Pieter | last post: by
7 posts views Thread by =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.