我想实现一个自动清除所有本地字段的接口,到目前为止,我有:
// 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);
如何设置最后一步以保存该字段的默认值?
类似于
clearMethodILGen.Emit(OpCodes.Ldfld, localField);
clearMethodILGen.Emit(OpCodes.Initobj, localField.FieldType);