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
Mattias Sjögren - 21 Apr 2006 06:22 GMT
>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:-
ldarg.0 loads 'this' in an instance method.
Mattias

Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Barry Kelly - 21 Apr 2006 09:16 GMT
> Please let me know how to load an object reference onto the stack in
> IL.
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!
-- Barry
Ben Voigt - 03 May 2006 19:36 GMT
> Hi
>
[quoted text clipped - 8 lines]
>
> ilgen.Emit(OpCodeToLoad, this);
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
> The this here refers to the CodeGen object reference, and at runtime
> will have to use the reference to call the CallBack method.
[quoted text clipped - 7 lines]
> Thanks
> Vivek Ragunathan