473,372 Members | 1,007 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,372 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 6605
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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...
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: 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
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...

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.