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

How to do Importing and exporting excel using C# and WPF?
Correct best-fit algorithm for bin packing?
LINQ - nested where clause
Dictionary<Tkey, TValue>, List<T> and other collections implementation / runtime
Asynchronous command execution with user confirmation
DevExpress TabbedView Freeze Focused Panel
Microsoft JScript runtime error: 'Sys' is undefined (in mvc3 c# razor)
how to Destroy all sessions at one Time in asp.net?
Place holder or watermark in TextBox windows 8
How to insert item into list in order?
Getting runtime error, C# Converting Array to Int
Covariance/Conversion issue with Generics
How do I keep track of a double and continue to add it each time a button is clicked?
DataGridTextColumn binding issue
Setting Default Value to a property on c# [duplicate]
How to evaluate the textblock binding while at timer tick

Categories

HOME
pact
compression
service
popover
ebay-api
thermal-printer
powerquery
swap
flowtype
web-testing
openstack-horizon
brightway
facebook-marketing-api
filehelpers
indri
printf
project-structure
udeploy
openoffice-basic
typedef
opengraph
v4l2
apdu
priority-queue
mex
multiple-inheritance
r-factor
schedule
key-bindings
kubectl
postgresql-9.6
apache-falcon
tunnel
cocoa-scripting
pymunk
motherboard
client-server
mongoid5
armadillo
paperclip
result
piranha-cms
spring-data-commons
hierarchical-clustering
ajv
aot
hellosign
business-catalyst
bipartite
scrollmagic
portaudio
zsh-completion
ldd
hoare-logic
angularfire
window.location
mod
web-optimization
video-embedding
firefox-os
sqldataadapter
musicbrainz
pdcurses
eula
vectordrawable
httpmodule
backtrace
qxorm
quickfixn
pysvn
powershell-v1.0
azure-worker-roles
im4java
serializer
angular-file-upload
behance-api
connections
codio
xml-validation
grunt-contrib-imagemin
violation
android-library
panelgrid
urlfetch
yii-components
webpage-screenshot
winrt-async
mysql-connector
scriptmanager
virtual-printer
xpolog
sipdroid
service-layer
moss2007-security
quazip
norton
llblgen
virtual-destructor
subdirectories
file-storage
wtsapi32

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