I am creating a dynamic type at runtime with the purpose of creating/serializing/deserializing objects from this type to then bind to a grid control. Everything works but my way of updating properties with records from the database is pathetic. I pasted this setter generator from somewhere and it works wonderfully when called via either delegate or PropertyInfo(...).SetValue(...)
ILGenerator setIl = setPropMthdBldr.GetILGenerator();
Label modifyProperty = setIl.DefineLabel();
Label exitSet = setIl.DefineLabel();
setIl.MarkLabel(modifyProperty);
setIl.Emit(OpCodes.Ldarg_0);
setIl.Emit(OpCodes.Ldarg_1);
setIl.Emit(OpCodes.Stfld, fieldBuilder);
setIl.Emit(OpCodes.Nop);
setIl.MarkLabel(exitSet);
setIl.Emit(OpCodes.Ret);
propertyBuilder.SetSetMethod(setPropMthdBldr);
But when I try to get cheeky and make my own version that increments rather than setting
ILGenerator incIl = incMethodBuilder.GetILGenerator();
incIl.Emit(OpCodes.Ldarg_0); //add object to stack
incIl.Emit(OpCodes.Ldfld, fieldBuilder); //add current field value to stack
incIl.Emit(OpCodes.Ldarg_1); //add method parameter to stack
incIl.Emit(OpCodes.Add); //combine last 2 items
incIl.Emit(OpCodes.Stfld, fieldBuilder); //write added value back
incIl.Emit(OpCodes.Nop); //no clue
incIl.Emit(OpCodes.Ret);
When I try to invoke this bad boy either via a delegate or even GetMethod(...).Invoke(...) I get the same fail
Common Language Runtime detected an invalid program.
Pretty obvious my il is wrong but I am not seeing the exact issue. I wouldn't even mind modifying the setter as the only thing that's ever done to these properties is that they're incremented
Also thanks in advance to anyone who comments that my design is bad
Aucun commentaire:
Enregistrer un commentaire