473,396 Members | 2,129 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,396 software developers and data experts.

Is there a way I can get that called my method from the call stack?

Hello

Using the System.Diagnostics.StackTrace class (see code below), I can know
that method that called my currently executing method. Is there a way I can
get a reference to the object instance used to call the method, i.e. the
this pointer for the calling method? I searched the FCL documentation for
such a method, but I didn't find any. Do I have to use Unmanaged code?

public class C2 {
public static void M1()
{
// here I can get the method that called my method
MethodInfo callingMethod = new StackTrace().GetFrame(1).GetMethod();
object callingObject; // I need to know how can I get a reference to
the calling object.
}
}

Note: My application will run using full trust, and normal user (not
Administrator) privileges.
Best regards,
Sherif
Nov 16 '05 #1
1 1037
> Using the System.Diagnostics.StackTrace class (see code below), I can know
that method that called my currently executing method. Is there a way I
can
get a reference to the object instance used to call the method, i.e. the
this pointer for the calling method? I searched the FCL documentation for
such a method, but I didn't find any. Do I have to use Unmanaged code?


If your method is called via COM interop the caller probalbly isn't managed
code and has no "this" reference. If your method is called from a project
which has been compiled with JIT optimizations enabled you have no guarantee
that the "this" reference will be on the stack. If your method is solely
called from managed code compiled without JIT optimizations, the reference
should be on the stack.
Unfortunantly, there is no way of finding this reference on the stack using
managed code. You could be able to gain access to the reference using the
Windows Debugging API, but again there are no guarantees that you'll find
the callers "this" reference and the debugging API is complicated to use.

I suggest that you just pass the callers reference as a parameter to the
method(s) where you need to access the callers instance.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/

Nov 16 '05 #2

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

Similar topics

4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
8
by: Rakehs | last post by:
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...
12
by: Tamir Khason | last post by:
How can I know who called the certain method of some object (from which class/method it was called) E.G. class A { Public A() { B b = new B(); }
1
by: Greif | last post by:
I am trying to call a static method within a sealed class. However, the wrong method keeps getting called instead. In a seperate class I am calling the following code: LogHelper.Log("Sample...
3
by: Nirbho | last post by:
Hi, I'm newish to C# .Net. I'm doing some error handling in my ASP.net pages. When handling the error in the catch block, I want to pass through the namespace of where the error occured.....
9
by: Steve | last post by:
Hello -- I've been struggling with this problem for over a day now. I'd like to know (without passing parameters) which class, and preferably, which method of that class has called my function....
35
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
25
by: Nicholas Parsons | last post by:
Howdy Folks, I was just playing around in IDLE at the interactive prompt and typed in dir({}) for the fun of it. I was quite surprised to see a pop method defined there. I mean is that a...
6
by: =?Utf-8?B?TWF0dA==?= | last post by:
I'm having a problem with a static class constructor being called twice. I have the static class MasterTaskList which uses a BackgroundWorker to execute multiple methods on a separate thread. The...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.