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

Who called me method???

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()"
Nov 16 '05 #1
12 2146
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]
- mv*@spam.guard.caspershouse.com

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:eW**************@TK2MSFTNGP09.phx.gbl...
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()"

Nov 16 '05 #2
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]
- mv*@spam.guard.caspershouse.com

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:eW**************@TK2MSFTNGP09.phx.gbl...
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()"

Nov 16 '05 #3
Great, thank you. The only one small thing leave. How to get the class of
method called?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Od**************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:eW**************@TK2MSFTNGP09.phx.gbl...
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()"


Nov 16 '05 #4
Great, thank you. The only one small thing leave. How to get the class of
method called?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Od**************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:eW**************@TK2MSFTNGP09.phx.gbl...
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()"


Nov 16 '05 #5

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Od**************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:eW**************@TK2MSFTNGP09.phx.gbl...
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()"


Nov 16 '05 #6

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Od**************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:eW**************@TK2MSFTNGP09.phx.gbl...
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()"


Nov 16 '05 #7
Got it. (GetMethod().ReflectedType ) tnx to all :)
"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:uK**************@TK2MSFTNGP09.phx.gbl...
Great, thank you. The only one small thing leave. How to get the class of
method called?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:Od**************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:eW**************@TK2MSFTNGP09.phx.gbl...
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()"



Nov 16 '05 #8
Got it. (GetMethod().ReflectedType ) tnx to all :)
"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:uK**************@TK2MSFTNGP09.phx.gbl...
Great, thank you. The only one small thing leave. How to get the class of
method called?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:Od**************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:eW**************@TK2MSFTNGP09.phx.gbl...
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()"



Nov 16 '05 #9
I need it for debugging in multithread enviroment. Are you know better
method to do it?

"Stu Smith" <st*****@nospam-digita.com> wrote in message
news:e9**************@TK2MSFTNGP11.phx.gbl...

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:Od**************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:eW**************@TK2MSFTNGP09.phx.gbl...
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()"



Nov 16 '05 #10
I need it for debugging in multithread enviroment. Are you know better
method to do it?

"Stu Smith" <st*****@nospam-digita.com> wrote in message
news:e9**************@TK2MSFTNGP11.phx.gbl...

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:Od**************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:eW**************@TK2MSFTNGP09.phx.gbl...
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()"



Nov 16 '05 #11
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

--
This is an automatic signature of MesNews.
Site : http://mesnews.no-ip.com

Nov 16 '05 #12
> 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

Nov 16 '05 #13

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

Similar topics

9
by: Felix Wiemann | last post by:
Sometimes (but not always) the __new__ method of one of my classes returns an *existing* instance of the class. However, when it does that, the __init__ method of the existing instance is called...
1
by: Sherif ElMetainy | last post by:
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...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
20
by: Charles Law | last post by:
I have an application that creates a class. The class has unmanaged resources, so must end gracefully. How can I guarantee that the unmanaged resources are freed? I have looked at IDisposable,...
10
by: Julia | last post by:
Hi Please can someone explain this behaviour: I have a MustInherit Base class and a Derived class that Inherits Base and Shadows a method in the base class. If I Dim a variable of type...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
67
by: Robert | last post by:
Hi, I have been reading the article at http://www.crockford.com/javascript/private.html and I was wondering if there also was some way to be able to have private methods that can be called from...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
6
by: bryanbabula | last post by:
I have a question about overriding i was wondering if anyone could help me with, or even suggesting a better/different way. I have no idea if this can even be done or not. I was wondering if there...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.