all groups > dotnet clr > april 2006 >
You're in the

dotnet clr

group:

Load an object reference onto the stack ???


Load an object reference onto the stack ??? VivekR
4/20/2006 4:11:25 PM
dotnet clr:
Hi

I have a class CodeGen in which GenerateILBody is a method that uses
ILGenerator.Emit to create the IL code as method body for a dynamic
method DynMethod. GenerateILBody at runtime must call another method
CallBack of the class CodeGen.

All i have to do is to emit the IL code that loads the object reference
of CodeGen onto the stack, which means I must emit IL in GenerateILBody
to load 'this', which may look like:-

ilgen.Emit(OpCodeToLoad, this);

The this here refers to the CodeGen object reference, and at runtime
will have to use the reference to call the CallBack method.

But there is no overload in ILgenerator.Emit that takes an object
reference.

Please let me know how to load an object reference onto the stack in
IL.

Thanks
Vivek Ragunathan
Re: Load an object reference onto the stack ??? Mattias Sjögren
4/21/2006 12:00:00 AM

[quoted text, click to view]

ldarg.0 loads 'this' in an instance method.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Re: Load an object reference onto the stack ??? Barry Kelly
4/21/2006 12:00:00 AM
[quoted text, click to view]

I have replied in a different newsgroup. I hopy you will consider
either crossposting instead of multiposting, or post in one, most
relevant group once. Thank you!

Re: Load an object reference onto the stack ??? Ben Voigt
5/3/2006 1:36:53 PM
[quoted text, click to view]

An object pointer can't be emitted because objects can be moved by the
garbage collector.

Two methods to refer to a particular object instance from inside your
emitted code (note you mustn't save the dynamic assembly to disk
afterwards):
(1) use a static field in the dynamic type, which you can assign using
reflection immediately after calling TypeBuilder.CreateType()

(2) use GCHandle. Write the integer returned by GCHandle.ToIntPtr as a
constant into the emitted code which then calls GCHandle.FromIntPtr

[quoted text, click to view]

AddThis Social Bookmark Button