Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / May 2006

Tip: Looking for answers? Try searching our database.

Load an object reference onto the stack ???

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
VivekR - 21 Apr 2006 00:11 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.