Get the calling type

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a way to find out what type called a certain method from another type? For instance let's say I have Type1.SomeMethod and I call if from Type2.SomeOtherMethod. From within SomeMethod how can I get the Type2 that called the method? I don't need the assembly only, but the exact type that made the call? I knew how to do this in C++, but I can't find anything in C#..

thanx,

iulian
 
I think you can accomplish this by using the StackTrace class:

http://tinyurl.com/37bkf
StackTrace Class
Represents a stack trace, which is an ordered collection of one or more
stack frames.


--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan
Iulian Ionescu said:
Is there a way to find out what type called a certain method from another
type? For instance let's say I have Type1.SomeMethod and I call if from
Type2.SomeOtherMethod. From within SomeMethod how can I get the Type2 that
called the method? I don't need the assembly only, but the exact type that
made the call? I knew how to do this in C++, but I can't find anything in
C#..
 
Thanks, it worked perfectly! Here's how I did it

StackFrame frm
StackTrace str
string s
str = new StackTrace()
for (int i = 0; i<str.FrameCount; i++

frm = str.GetFrame(i)
s = frm.GetMethod().Name


The first Frame always contains the currently running method and the second frame contains the calling method name..

thanx
iulia
 
=?Utf-8?B?SXVsaWFuIElvbmVzY3U=?= said:
Thanks, it worked perfectly! Here's how I did it:

StackFrame frm;
StackTrace str;
string s;
str = new StackTrace();
for (int i = 0; i<str.FrameCount; i++)
{
frm = str.GetFrame(i);
s = frm.GetMethod().Name;
}

The first Frame always contains the currently running method and the
second frame contains the calling method name...

No, that's not true. It's true when the JITter hasn't removed stack
frames due to inlining, but in general it's not true. Basically, the
stack can't be relied upon to be absolutely accurate.

To the OP: There's no guaranteed way of doing this. Usually the best
thing is to change your design so you don't need this information.
 
Hi Iulian,

You can use StackTrace class' methods to get that information. But be
advised that due to optimizations made by c# and the JITter you may not get
the same results as you expect looking in the source codes.

Example

class Foo
{
private Bar mBar = new Bar();

public void TestFoo()
{
StackTrace st = new StackTrace(false);
if(st.FrameCount > 1)
{
StackFrame sf = st.GetFrame(1);
Console.WriteLine("TestFoo called from: {0}",
sf.GetMethod().ReflectedType.Name);
}
mBar.TestBar();
}
}
//================================================
class Bar
{
public void TestBar()
{
StackTrace st = new StackTrace(false);
if(st.FrameCount > 1)
{
StackFrame sf = st.GetFrame(1);
Console.WriteLine("TestBar called from: {0}",
sf.GetMethod().ReflectedType.Name);
}
}
}
//=======================================
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Foo foo = new Foo();
foo.TestFoo();
Console.ReadLine();
}
}
//==============================================
Results:
TestFoo called from: Class1
TestBar called from: Foo

HTH
B\rgds
100

Iulian Ionescu said:
Is there a way to find out what type called a certain method from another
type? For instance let's say I have Type1.SomeMethod and I call if from
Type2.SomeOtherMethod. From within SomeMethod how can I get the Type2 that
called the method? I don't need the assembly only, but the exact type that
made the call? I knew how to do this in C++, but I can't find anything in
C#..
 
Back
Top