Emit Codes field to set value

I am trying to dynamically create a proxy, so I am trying to use Emit. So when I set my field using emit, I also need to set the isDirty field boolan to true.

How can i do this?

Property Customer { set { this.customerName = value; this.isDirty = true; } } 

emit code:

  FieldBuilder isDirtyField = myTypeBuilder.DefineField("isDirty", typeof(bool), FieldAttributes.Private); // Define the "set" accessor method for CustomerName. MethodBuilder custNameSetPropMthdBldr = myTypeBuilder.DefineMethod("set_CustomerName", getSetAttr, null, new Type[] { typeof(string) }); ILGenerator custNameSetIL = custNameSetPropMthdBldr.GetILGenerator(); custNameSetIL.Emit(OpCodes.Ldarg_0); custNameSetIL.Emit(OpCodes.Ldarg_1); custNameSetIL.Emit(OpCodes.Stfld, customerNameBldr); { custNameSetIL.EmitWriteLine("Start isDirty"); ... do stuf here custNameSetIL.EmitWriteLine("End isDirty"); } custNameSetIL.Emit(OpCodes.Ret); 

This code works, since for a long time I am not trying to make this isDirty field, after spending a weekend on this, I am trying to get some help on this forum. THX

// dennis

+6
source share
1 answer

I believe that the sequence of IL instructions you want will be

 custNameSetIL.Emit(OpCodes.Ldarg_0); // load this custNameSetIL.Emit(OpCodes.Ldc_I4_1); // load true (same as integer 1) custNameSetIL.Emit(OpCodes.Stfld, isDirtyField); // store into isDirty 
+7
source

Source: https://habr.com/ru/post/891920/


All Articles