Obtaining Caller of a Method in C#
-
I was wondering if there is any (preferably relatively straighforward) way for a method in C# to obtain the object which made the call. For example...
namespace Example
{
public class A
{private B b; public A() { b = new B(); b.Call(); }
}
public class B
{
public B()
{
}public void Call() { //So, who called this function? object caller = null; // Replace null with what so that // we get the instance of A making // the call. }
}
}
I hope this makes some sense. Any hints / tips are much appreciated. -- The Last Bastion
-
I was wondering if there is any (preferably relatively straighforward) way for a method in C# to obtain the object which made the call. For example...
namespace Example
{
public class A
{private B b; public A() { b = new B(); b.Call(); }
}
public class B
{
public B()
{
}public void Call() { //So, who called this function? object caller = null; // Replace null with what so that // we get the instance of A making // the call. }
}
}
I hope this makes some sense. Any hints / tips are much appreciated. -- The Last Bastion
-
I was wondering if there is any (preferably relatively straighforward) way for a method in C# to obtain the object which made the call. For example...
namespace Example
{
public class A
{private B b; public A() { b = new B(); b.Call(); }
}
public class B
{
public B()
{
}public void Call() { //So, who called this function? object caller = null; // Replace null with what so that // we get the instance of A making // the call. }
}
}
I hope this makes some sense. Any hints / tips are much appreciated. -- The Last Bastion
Have a look at the stack trace objects in the debugging namespace, cant remember offhand where it is (not on a .NET Pc at the moment!) System.Diagnostics might be the one. Not 100% sure if it returns the object, or just a list of methods called. Cheers, James James Simpson Web Developer imebgo@hotmail.com
-
Have a look at the stack trace objects in the debugging namespace, cant remember offhand where it is (not on a .NET Pc at the moment!) System.Diagnostics might be the one. Not 100% sure if it returns the object, or just a list of methods called. Cheers, James James Simpson Web Developer imebgo@hotmail.com
-
I was typing that, then i realized, hey where is the object? :doh: leppie::AllocCPArticle("Zee blog");
leppie wrote: hey where is the object? There isn't one, also what would you do if the caller was a static method? In that case there is no object reference that is doing the calling. AFAIK, the best you can hope for is to get the class/struct name and possibly get the
Type
object representing it. James At Jethro Tull's August 28, 2003 concert Ian Anderson mentioned that the group would be performing a medley of title tracks. The songs were "Songs from the Wood", "Too Old to Rock and Roll; Too Young to Die"; and from the Heavy Horses album, "Stairway to Heaven". -
I was wondering if there is any (preferably relatively straighforward) way for a method in C# to obtain the object which made the call. For example...
namespace Example
{
public class A
{private B b; public A() { b = new B(); b.Call(); }
}
public class B
{
public B()
{
}public void Call() { //So, who called this function? object caller = null; // Replace null with what so that // we get the instance of A making // the call. }
}
}
I hope this makes some sense. Any hints / tips are much appreciated. -- The Last Bastion
My article on custom trace listeners has a decent write-up on this: TraceListeners and Reflection Towards the end there's a section on "StackFrame and StackTrace", with some example code. Jerry Dennany