This is the first time I'm dabbling with generated CIL, so please bear with my ignorance. I'm looking for a simple DynamicMethod that can read the fields of a POCO, and fill them into an object[]
. No type conversion is necessary. I've put together everything I can, can you help complete it?
Type t = typeof(POCO);
DynamicMethod dm = new DynamicMethod("Get" + memberName,typeof(MemberType), new Type[] { objectType }, objectType);
ILGenerator il = dm.GetILGenerator();
// Load the instance of the object (argument 0) onto the stack
il.Emit(OpCodes.Ldarg_0);
// get fields
FieldInfo[] fields = t.GetFields();
// how do I create an array (object[]) at this point?
// per field
foreach (var pi in fields) {
// Load the value of the object's field (fi) onto the stack
il.Emit(OpCodes.Ldfld, fi);
// how do I add it into the array?
}
// how do I push the array onto the stack?
// return the array
il.Emit(OpCodes.Ret);