[Mono-devel-list] Re: Find Object instance that called a method..

Robert Jordan robertj at gmx.net
Wed Jun 22 04:57:36 EDT 2005


S Umadevi wrote:

> I guess I phrased my question too simplicitly...:(
> Anyway I found my answer with System.Diagonistics.StackFrame.

StackFrame will not provide you the object *instance*
of the caller, just the its MethodInfo and so its class.

BTW, you're calling for big trouble because methods often
get inlined at runtime. It this case the StackFrame will
omit the method and your code will probably break.
This happens reliably on MSFT's runtime in RELEASE mode
wenn runing the code w/out a debugger attached.
I suspect that Mono behaves similar.

Rob

> 
> Thanks
> uma
> 
> 
> 
>>>>Micha(B³ Ziemski <rook at roo.k.pl> 06/21/05 2:53 PM >>>
> 
> 
>>Hi,
>>Is there a mechanism to figure out the object instance that called a
>>method..
>>Ex.
>>class A {
>>           
>>public  void m1()
>>{
>> //some code..
>>}
>>
>>}
>>
>>Class B {
>>
>> private m2()
>>{
>>  A aa = new A();
>>   aa.m1();
>>
>>}
>>}
>>
>>>From method m1 in class A is it possible for me to figure out the
>>object instance aa?
>> 
>>
> 
> Use "this" keyword (I hope this is what you are asking for).
> 
> public void m1() {
>     this.ToString();
> }
> 
> Regards!
> Michal Ziemski
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com 
> http://lists.ximian.com/mailman/listinfo/mono-devel-list




More information about the Mono-devel-list mailing list