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

Why am I getting System.OutOfMemoryException?
Change display of DateTime in MVC
XML attribute to indicate overwrite vs. preserve existing value
EdmSchemaAttribute interrupt some pages and throws an exception
Setting a property hidden by control's interface
EF Code First 4.1: “Multiple Inheritence” (Is A) Question
TripleDESCryptoServiceProvider using hex key
Use SQL Server CE Locally and SQL Server 2008 on Production Entity Framework Code First?
CRM 2011 SecurityNegotiationException trying to access web services
How to place child controls in a certain order when binding a custom control?
How to get rid of the exception “The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine” in Win 7
How to position a form on top of another form without taking the focus
filter IQueryable in a loop with multiple Where statements
How to write value to html field from asp.net code behind
facebook graph api: /me OAuthException (c#)
Changing input value using javascript

Categories

HOME
marie
alasql
display
glsl
mifare
boxplot
spotfire
magento-1.9.2.1
assign
ubuntu-14.04
hiveql
ebay
zabbix
handlebars.js
samsung-gear-s2
box-api
theory
gcloud
unreal-engine4
xmlhttprequest
javascript-debugger
opendaylight
box2d
filehelpers
prepros
core-animation
gulp-jshint
tikz
blockly
storefront
epub
aweber
perf
android-databinding
file-descriptor
electron-packager
uitextview
peerjs
xmldom
stanford-nlp-server
ng-class
e4
fluent-nhibernate-mapping
spring-cloud-aws
acceleo
pyautogui
vertex-shader
boost-range
rowname
mate-desktop
nimbus
database-administration
user-profile
paypal-subscriptions
methodology
jsondoc
kramdown
tvp
libgcrypt
play-json
serial-communication
textblob
dcg
vectordrawable
service-broker
nexusdb
cl.exe
backtrace
crtdbg.h
caption
apache-pivot
fscanf
rda
image-preloader
android-jack-and-jill
code-complete
dylan
kendo-window
kohana-orm
antixsslibrary
qt5.1
ncml
nsregularexpression
invalidoperationexception
xcode4.6.3
win-prolog
jqzoom
automapper-2
file-processing
moss2007-security
marathontesting
subdirectories
database-agnostic
user-preferences
ffdshow

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