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#..