Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to get a reference to the calling object in c#?

Tags:

What I'm wondering is if it's possible to (for instance) to walk up the stack frames, checking each calling object to see if matches an interface, and if so extract some data from it.

Yes, I know it's bad practice, I'm wondering if it's possible.

like image 553
Andrew Ducker Avatar asked Jan 07 '09 14:01

Andrew Ducker


People also ask

How to call by reference in c#?

Here, we have added the ref keyword with the input parameter that means when we call this method, the argument must be passed by reference. Then we call the UpdateValue method but while calling, we have to use the ref keyword before the argument name. This is Call by Reference in C# with Value Type.

When to use this in c3?

The this keyword is used to reference the current instance of a class, or an object itself, if you will. It is also used to differentiate between method parameters and class fields if they both have the same name.

What is a calling object?

"Calling object" is the object that calls the method . – debashish. Jul 2, 2017 at 3:27. The "calling object" is passed implicitly as the first argument (e.g., as "self" in its class definition): object1. method(object2) is the same as method(object1, object2) .


1 Answers

No, there isn't - at least not without using a profiling/debugging API of some description. You can walk the stack to find the calling method, with the caveat that it's really slow and may be inaccurate due to JIT optimisations. That won't tell you what the calling object is though (if indeed there is one).

like image 194
Jon Skeet Avatar answered Sep 23 '22 01:09

Jon Skeet