Re: Load an object reference onto the stack ???
- From: Barry Kelly <barry.j.kelly@xxxxxxxxx>
- Date: Fri, 21 Apr 2006 09:19:11 +0100
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
.
- References:
- Load an object reference onto the stack ???
- From: VivekR
- Load an object reference onto the stack ???
- Prev by Date: Re: Load an object reference onto the stack ???
- Next by Date: Re: FileSystemWatcher.Created checks for completed files?
- Previous by thread: Re: Load an object reference onto the stack ???
- Next by thread: Random appdomain restarts
- Index(es):
Relevant Pages
|