I want to implement an interface that automatically clears all local fields, so far I have:
// Implement IClearable
dynamicType.AddInterfaceImplementation(typeof(IClearable));
MethodBuilder clearnMethodBuilder = dynamicType.DefineMethod("Clear", MethodAttributes.Public | MethodAttributes.Virtual, CallingConventions.Standard);
ILGenerator clearMethodILGen = clearnMethodBuilder.GetILGenerator();
foreach (FieldBuilder localField in fields)
{
clearMethodILGen.Emit(OpCodes.Ldarg_0);
clearMethodILGen.Emit(OpCodes.Ldfld, localField);
clearMethodILGen.Emit(OpCodes.??, Profit??);
}
clearMethodILGen.Emit(OpCodes.Ret);
How do I set the last step to save the default value over the field?
something like:
clearMethodILGen.Emit(OpCodes.Ldfld, localField);
clearMethodILGen.Emit(OpCodes.Initobj, localField.FieldType);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With