Who called me method???

  • Thread starter Thread starter Nicholas Paldino [.NET/C# MVP]
  • Start date Start date
N

Nicholas Paldino [.NET/C# MVP]

Tamir,

In order to do this, you will have to instantiate a new instance of the
StackFrame class. You need to pass 1 to the constructor to let it know to
go up one frame in the stack. Once you have that, you can call the
GetMethod method to get the MethodBase implementation representing the
method that called you. Finally, use the Name property to get the name.

Hope this helps.
 
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();
}
Private Some()
{
b.Caller();
}
}
Class B
{
Public Caller()
{
Console.WriteLine ("Called from {0}",WHO_CALL_ME);
}
}

Output: "Called from A.Some()"
 
Nicholas Paldino said:
Tamir,

In order to do this, you will have to instantiate a new instance of the
StackFrame class. You need to pass 1 to the constructor to let it know to
go up one frame in the stack. Once you have that, you can call the
GetMethod method to get the MethodBase implementation representing the
method that called you. Finally, use the Name property to get the name.

And furthermore, you'll need to disable inlining for the method(s) as
appropriate (see MethodImplAttribute).

Might I ask why you want this? It will be awkward and slow. Perhaps you
could define some sort of interface that the callee takes, with the callers
constructing the appropriate object.

Stu
Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tamir Khason said:
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();
}
Private Some()
{
b.Caller();
}
}
Class B
{
Public Caller()
{
Console.WriteLine ("Called from {0}",WHO_CALL_ME);
}
}

Output: "Called from A.Some()"
 
Great, thank you. The only one small thing leave. How to get the class of
method called?

Nicholas Paldino said:
Tamir,

In order to do this, you will have to instantiate a new instance of the
StackFrame class. You need to pass 1 to the constructor to let it know to
go up one frame in the stack. Once you have that, you can call the
GetMethod method to get the MethodBase implementation representing the
method that called you. Finally, use the Name property to get the name.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tamir Khason said:
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();
}
Private Some()
{
b.Caller();
}
}
Class B
{
Public Caller()
{
Console.WriteLine ("Called from {0}",WHO_CALL_ME);
}
}

Output: "Called from A.Some()"
 
Got it. (GetMethod().ReflectedType ) tnx to all :)


Tamir Khason said:
Great, thank you. The only one small thing leave. How to get the class of
method called?

message news:[email protected]...
Tamir,

In order to do this, you will have to instantiate a new instance of the
StackFrame class. You need to pass 1 to the constructor to let it know to
go up one frame in the stack. Once you have that, you can call the
GetMethod method to get the MethodBase implementation representing the
method that called you. Finally, use the Name property to get the name.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tamir Khason said:
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();
}
Private Some()
{
b.Caller();
}
}
Class B
{
Public Caller()
{
Console.WriteLine ("Called from {0}",WHO_CALL_ME);
}
}

Output: "Called from A.Some()"
 
I need it for debugging in multithread enviroment. Are you know better
method to do it?



Stu Smith said:
message news:[email protected]...
Tamir,

In order to do this, you will have to instantiate a new instance of the
StackFrame class. You need to pass 1 to the constructor to let it know to
go up one frame in the stack. Once you have that, you can call the
GetMethod method to get the MethodBase implementation representing the
method that called you. Finally, use the Name property to get the name.

And furthermore, you'll need to disable inlining for the method(s) as
appropriate (see MethodImplAttribute).

Might I ask why you want this? It will be awkward and slow. Perhaps you
could define some sort of interface that the callee takes, with the callers
constructing the appropriate object.

Stu
Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tamir Khason said:
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();
}
Private Some()
{
b.Caller();
}
}
Class B
{
Public Caller()
{
Console.WriteLine ("Called from {0}",WHO_CALL_ME);
}
}

Output: "Called from A.Some()"
 
Hi,
Use MethodBase.GetCurrentMethod() or (new
StackTrace()).GetFrame(1).GetMethod() and convert that to MethodInfo
type. Then retrieve the information regarding that method.

Regards
Zafar Iqbal
 
In order to do this, you will have to instantiate a new instance of
the
StackFrame class. You need to pass 1 to the constructor to let it know to
go up one frame in the stack. Once you have that, you can call the
GetMethod method to get the MethodBase implementation representing the
method that called you. Finally, use the Name property to get the name.

This method is VERY slow, so be sure you don't do it in a performance
critical section of your code, and benchmark the result to be sure it's
acceptable.

Eric
 
Back
Top