.NETGURU
How to get caller's "this"?
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.clr.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Greg Ennis (VIP)
Given this code

class A { void Foo() { B b = new B(); b.Bar(); } }
class B { void Bar() { ... } }

In the body of the Bar() method, how can it get a reference to the instance
of the "A" object that called it? I am not interested in the method
information or type information of the object... I need a reference to the
actual instance of "A". It must be possible with reflection or diagnostics,
but I can't seem to figure out how. (Obviously, I am trying to avoid having a
parameter for this)

Thanks

Reply to this message...
 
    
David Browne
"Greg Ennis" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

You can't, at least not without using the debugger. If B needs a reference
to A, pass it as a method parameter, or a constructor parameter or a static
variable.

David

Reply to this message...
 
    
Greg Ennis (VIP)
[Original message clipped]

There must be a way to do it... The this pointer has to be stored like any
other parameter in the stack frame of the caller. How to get it?

Reply to this message...
 
    
Ben Schwehn
[Original message clipped]

why can't you use a static variable?
Set it in RealClass and read it in either ProxyClass or EventHandler.
As long as all is singlethreaded it could work something like this:

#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;

public __value class Reference {
public:
    static Object* ref;
};

public __gc class Callee
{
public:
Callee(){}
void Call() {
     Object* refToParent = Reference::ref;
     Console::WriteLine(refToParent->ToString());
}
};

public __gc class Caller
{
public:
Caller(){}
void DoCall() {
     Reference::ref = this;
     Callee* callee = new Callee();
     callee->Call();
     }
};

int _tmain()
{
    Caller* caller = new Caller();
    caller->DoCall();
    return 0;
}

--
Ben
http://bschwehn.de
Reply to this message...
 
    
Daniel O'Connell [C# MVP] (VIP)
"Greg Ennis" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

No, unfortunatly, there is no simple way to do this. You will have to pass a
parameter or design around it.

Its infeasible really, since you can't know what type calls you(class B may
be used in class A and class TwentySix for all you know). How would you
reference that value? And how would you access the callers parent?
It is a curious problem, but I do not believe it is possible.

Reply to this message...
 
    
Greg Ennis (VIP)
"Daniel O'Connell [C# MVP]" wrote:

[Original message clipped]

Actually I don't care what the type is. I just need to refer to it as
"object". Here is the situation and why I believe it must be possible
somehow. I have a proxy class that is calling an event delegate. As you know
these event handler delegates take a "sender" parameter as well the
"EventArgs". Now, the sender parameter needs to be the actual sender, not the
proxy class. So:

RealClass -> ProxyClass -> EventHandler ->... event handlers

The problem is that I need "sender" of the event to be the RealClass,
although I "fire" the event in ProxyClass. I can not change the method
signature to add a parameter for the sender because it is a generated proxy
class.

Any help is appreciated. Thanks

Reply to this message...
 
    
Rick Byers [MSFT] (VIP)
You're right that the 'this' pointer should be on the stack somewhere, but
there is no way to try to find it using managed code. You could probably
get it using the debugging API, but there is no guarantee that it could be
found - especially if JIT optimizations are enabled. Also, there are other
ways for a managed function to be called than just from some other managed
object (eg. reverse PInvoke, COM interop, etc.). If you try to sniff this
from the stack, it's going to be unreliable and brittle.

What are you using the 'sender' for? In this case, if the proxy isn't
designed to be "transparent", then it kind of is the sender, so it sounds
like you technically want something other than "sender". In that case, it
feels like the "right" way to solve this problem is to add a new argument
to the EventArgs that passes the information. If the proxy is supposed to
be transparent, then it shouldn't be changing the sender. Do you have any
control over the RealClass, or just the event handler? If you can't change
the RealClass or ProxyClass, then you are pretty much stuck. Who generates
the ProxyClass? This isn't a CLR Transparent proxy, is it (I think it
would do the correct "transparent" thing and not change the sender)? Any
chance you can customize the proxy generation somehow?

Rick

--
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
[Original message clipped]

Reply to this message...
 
 
System.EventArgs
System.EventHandler




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification