public Converter WithEvents() { // TODO: if (AddedEvents || Wrapped.IsRegMember(RegPairs, out _)) { return(this); } foreach (var e in Wrapped.GetEvents()) { var eventName = e.Name; var eventType = e.EventHandlerType .GetGenericArguments() .FirstOrDefault(); // Creates an Action event that doesn't require ActionProxy when subscribing var newEvent = CreateEvent(eventName); // Create method a which gets called when the Action event fires, and forwards the event to the normal C# event string methodName = "Forward" + eventName + "Event"; CodeMemberMethod newMethod = CodeDomEx.CreateMethod(methodName, typeof(void)); if (eventType == null) { AddVoidActionEvent(newEvent, newMethod); } else { AddActionEventWithRetVal(newEvent, eventType, newMethod); } Klass.Members.Add(newEvent); Klass.Members.Add(newMethod); // Add statements to the constructor to invoke the new event when the event in the // wrapped object fires. var actionProxyType = eventType == null ? "SuperMemoAssistant.Sys.Remoting.ActionProxy" : $"SuperMemoAssistant.Sys.Remoting.ActionProxy<{eventType.FullName}>"; CodeDelegateCreateExpression createDelegate1 = new CodeDelegateCreateExpression( new CodeTypeReference(actionProxyType), new CodeThisReferenceExpression(), methodName); CodeEventReferenceExpression actionEventRef = new CodeEventReferenceExpression( WrappedRef, eventName); // Attaches an EventHandler delegate pointing to TestMethod to the TestEvent event. CodeAttachEventStatement attachStatement1 = new CodeAttachEventStatement(actionEventRef, createDelegate1); Constructor.Statements.Add(attachStatement1); } AddedEvents = true; return(this); }