Trouble Emitting Object Array using Reflection.Emit
Posted
by JoeGeeky
on Stack Overflow
See other posts from Stack Overflow
or by JoeGeeky
Published on 2010-04-04T11:40:30Z
Indexed on
2010/04/04
11:43 UTC
Read the original article
Hit count: 366
I am trying to Emit what I thought would be a simple object array that would result in code similar to the below example
object[] parameters = new object[] { a, b, };
When I write the above code in C# using VS, I get the following IL. As expected this works.
.locals init (
[0] object[] parameters,
[1] object[] CS$0$0000)
However, when I try and Emit IL directly, I only ever get a one index init array. Can someone help tell me where I've gone wrong here?
Here is the Emit code I'm using:
int arraySize = 2;
LocalBuilder paramValues = ilGenerator.DeclareLocal(typeof(object[]));
paramValues.SetLocalSymInfo("parameters");
ilGenerator.Emit(OpCodes.Ldc_I4_S, arraySize);
ilGenerator.Emit(OpCodes.Newarr, typeof(object));
ilGenerator.Emit(OpCodes.Stloc, paramValues);
Here is the resulting IL:
.locals init (
[0] object[] objArray)
The rest of the resulting IL is identical between the two solutions, but for some reason the .locals init is different.
© Stack Overflow or respective owner