Trouble Emitting Object Array using Reflection.Emit
- by JoeGeeky
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.