c#


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.GetĀ­Frame 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.

Related Links

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

Categories

HOME
asterisk
asp.net
pact
watson-iot
itext7
arguments
crash
symfony-sonata
mediarecorder
multipartform-data
hibernate-mapping
scapy
rmarkdown
tracking
saucelabs
multipart
wai-aria
scrapy-spider
nsopenpanel
rollupjs
stack-trace
uibarbuttonitem
spring-data-gemfire
2checkout
paper-trail-gem
openoffice-impress
wordpress-rest-api
login-required
directions
snapkit
asp.net-webhooks
flex3
redhat-brms
framebuffer
area
ui-grid
azure-web-roles
ape-phylo
definition
amazon-fire-tv
jks
spring-data-commons
openpgp.js
breadcrumbs
blitz3d
directinput
android-music-player
zsh-completion
visual-web-developer
boost-range
queuing
angular-i18n
innertext
containstable
web-performance
magnet-uri
ruby-1.8.7
bootstrap-tabs
roslyn-code-analysis
android-filterable
joomla3.3
tidesdk
ultraedit
service-broker
google-experiments
method-overloading
system.io.packaging
worklight-appcenter
angular-file-upload
django-tests
elixir-framework
.net-reflector
xtify
facebook-likebox
maven-javadoc-plugin
tlbimp
ojb
qmainwindow
esi
shapado
jexcelapi
norton
cstring
high-load

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App