I would like to use reflection in .NET to get access to the object that invoked my method. I assume it is somehow possible to view up the stacktrace. I know it's not safe for a variety of reasons, but I just need to grab and catalog some property values.
How do I do this?
Update: I'm an idiot, I forgot to say this was in C#
var methodInfo = new StackFrame(1).GetMethod();
Returns the method that called the current method.
Note that the compiler may inline or otherwise optimize calls to methods (although that sounds like it might not be the case for you), which thwarts expected behavior. To be safe, decorate your method with:
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
Note that this goes contrary to allowing the compiler to do its job. Caveat emptor.
EDITED Oops, I see that you want the instance that invoked your method. There's no way to get that information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With