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()" 12 2148
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()"
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()"
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()"
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()"
"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()"
"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()"
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()"
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()"
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()"
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()"
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
> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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...
| |