I am currently using this post as a guide How to create multi-dimensional array using Reflection.Emit but using a single dimensional array.
My function looks like
public void CallMethod3(object[] Items)
{
}
and my reflection emit code looks like
MethodInfo invokerMethod = typeof(Foo).GetMethod("CallMethod3");
il.Emit(OpCodes.Nop);
il.Emit(OpCodes.Ldarg_0);
// var local = new int[1];
LocalBuilder local = il.DeclareLocal(typeof(object[]));
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Newobj, typeof(object[]).GetConstructor(new[] { typeof(int) }));
il.Emit(OpCodes.Stloc, local);
// il.Emit(OpCodes.Ldloc, local);
// il.Emit(OpCodes.Ldc_I4_0);
// il.Emit(OpCodes.Ldc_I4, 123);
// var setMethod = typeof(object[]).GetMethod("Set");
// il.Emit(OpCodes.Call, setMethod);
il.Emit(OpCodes.Ldloc, local);
il.Emit(OpCodes.Call, invokerMethod);
il.Emit(OpCodes.Ret);
Everything works fine and I can hit the breakpoint at the method. However, I start facing problem when I try to populate the object array (code commented out here). I start getting invalid invocations.
Any idea what I am doing wrong?