public void ProcessEvent(EventDefinition eventt) { _logger.LogDebug("Beginning processing event " + eventt.Name); FieldReference eventDelegate = eventt.GetEventDelegate(); if (eventDelegate != null) { if (eventDelegate.FieldType.IsValidEventDelegate()) { WeakEventWeaver weakEventWeaver = new WeakEventWeaver(eventDelegate, _moduleImporter); ProcessAddMethod(eventt.AddMethod, weakEventWeaver); ProcessRemoveMethod(eventt.RemoveMethod, weakEventWeaver); } else { _logger.LogInfo("Skipping event " + eventt + ", incompatible event delegate type"); } } else { _logger.LogInfo("Skipping event " + eventt + ", could not determine the event delegate field"); } _logger.LogDebug("Finished processing event " + eventt.Name); }
private static void ProcessAddMethod(MethodDefinition addMethod, WeakEventWeaver weakEventWeaver) { var weakEventHandler = weakEventWeaver.CreateEventHandlerVariable(addMethod); var makeWeak = weakEventWeaver.GenerateMakeWeakIl(addMethod, weakEventWeaver.AddUnsubscribeMethod(), weakEventHandler); int oldCodeIndex = addMethod.InsertInstructions(makeWeak, 0); // Now replace any further use of the method parameter (Ldarg_1, or Ldarg_0 if static) with the weak event handler var instructionToReplace = GetMethodLoadFirstArgumentCode(addMethod); var instructions = addMethod.Body.Instructions; for (int i = oldCodeIndex; i < instructions.Count; i++) { if (instructions[i].OpCode.Code.Equals(instructionToReplace)) { instructions[i] = Instruction.Create(OpCodes.Ldloc, weakEventHandler); } } }