getting the caller of a method in c#
is there a way to get the instance of class that called some method?
You can get the name of the method that calls by examining the call stack. Getting the class instance is a different story, and is not easily achieved (if even possible; I never really tried to do it). You should for instance consider the possibility that your method is called from a static method, in which case there is no class instance to find. Either way, this is usually not a good thing to do. If the method needs access to whatever class instance that calls into it, add that as a parameter in the method.
Not really. While you can find some information on the stack, there are several situations where there is no instance, such as managed-unmanaged code boundaries (windows messages, callbacks, COM-interop etc.) or simple static methods as well. Maybe you should explain what you are trying to achieve in the first place, so that alternative solutions can be found.
You can check the StackTrace.GetFrame method. :)
Well if you are in the method, you can use this line to get the type of the class. But I'm not sure if it is really what you want since you ask for the instance. Type yourclass = MethodBase.GetCurrentMethod().DeclaringType;
If possible, you could try adding an object sender parameter as is common amongst WPF methods for example.
WPF TabItem border isn't reflecting BorderThickness property
Fastest way of getting properties of ManagementObject
MS Bot builder IForm Invalid Type Exception:Has xxx Expect yyy
Table.compute() C# MIN, MAX and AVG
Post Build Event: Build Class Library and Copy Bin Folder Contents
Loading a Large Form in MVC
Encrypt/Decrypt String for Functions [duplicate]
How to set a different language to Bot Framework in Facebook Channel?
SQL display table in vertical
how to converter unreadable block of code in a class
Practical examples of when to use a ListPool or any pool of collections
I want to take array of 30 and use 3 threads that will sum the 30 values.Each thread adds in groups of 10
Send data from ASP.NET MVC to desktop application
What is the APEv1 tag identifier?
ServiceStack Client Put request and query parameters
using cookies for session mvc