Re: Load an object reference onto the stack ???

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On 20 Apr 2006 16:13:11 -0700, "VivekR" <KenBase@xxxxxxxxx> wrote:

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

You can't, because IL is designed for storage in assemblies, and
objects currently need to live on the heap.

The way I get the behaviour you're after is by using an object[]
reference as the implicit 'this' argument to the delegate definition.
There are two DynamicMethod.CreateDelegate() overloads, and one of
them takes an object which will be stored in the created delegate as
the 'this' reference.

public Delegate CreateDelegate (
Type delegateType,
Object target
)

I pass the object[] as the second argument to this method. The
delegateType doesn't include this object[] in the signature, however,
from the MSDN docs:

delegateType
A delegate type whose signature matches that of the dynamic method,
minus the first parameter.

Using this technique, you can create a delegate which you call as
normal, but it gets passed the object[] automatically.

When generating code, you need to write something like this:

int refIndex = refs.Count;
refs.Add(myRef.Value);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldc_I4, refIndex);
il.Emit(OpCodes.Ldelem_Ref);
if (myRef.Type.IsValueType)
il.Emit(OpCodes.Unbox_Any, myRef.Type);
else
il.Emit(OpCodes.Castclass, myRef.Type);

This code is taken from my own code generators, where myRef is a pair
of (object reference, object type). You need the type because the
reference might be null.

Then at the end, you get your object[] from refs.ToArray().

HTH,

-- Barry

-- Barry
.



Relevant Pages

  • Re: Generic Delegate Types explained (example)
    ... Unless you have specifically stated that the generic type has to be a value type, it can always be a reference type. ... To change the value of a string you have to replace it with a new string instance, which you can not do unless you send the string by reference to the method. ... example of generic delegate types taken from p. 108 of the C# 3.0 ... static Util UtilStringChanger2AndMoreStaticVersion ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: multithreaded tcp/ip monitoring application
    ... Passing a copy of a reference type variable just passes the reference, and so mutations in the instance can still be seen by multiple threads. ... The string type is a reference type, but a string instance is immutable and the string class is thread-safe. ... the variable itself is mutable and that's why you need the synchronization. ... But, and this is very important, the "str" variable itself is not referenced by the invoked delegate. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Threads & Garbage Collection
    ... A delegate contains a member called Target. ... giving out a reference to your object. ... valid reference to your management object until the thread goes away. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Load an object reference onto the stack ???
    ... The IL I generated calls a passed-in delegate. ... have a method called "Invoke". ... Load the object reference onto the stack. ...
    (microsoft.public.dotnet.framework)
  • Re: RaiseEvent
    ... // Store the reference to the delegate. ... assign a default delegate to the event that points to empty code. ... Making OnClick virtual also allows the derived class ... > public event EventHandler Click; ...
    (microsoft.public.dotnet.languages.csharp)