/// <summary> /// Defines an event based on the specified event. /// </summary> /// <param name="typeBuilder">The type builder.</param> /// <param name="eventInfo">The event information.</param> /// <param name="isExplicit">A value indicating whether the specified event should be implemented explicitly.</param> /// <param name="methodBuilderFactory">The method builder factory function.</param> /// <returns>The event builder.</returns> public static void DefineEvent(this TypeBuilder typeBuilder, EventInfo eventInfo, bool isExplicit, Func<MethodInfo, bool, MethodBuilder> methodBuilderFactory) { if (typeBuilder == null) throw new ArgumentNullException("typeBuilder"); if (eventInfo == null) throw new ArgumentNullException("eventInfo"); if (methodBuilderFactory == null) throw new ArgumentNullException("methodBuilderFactory"); // Define event. var eventName = isExplicit ? eventInfo.GetFullName() : eventInfo.Name; var eventBuilder = typeBuilder.DefineEvent( eventName, eventInfo.Attributes, eventInfo.EventHandlerType); // Build event add method. var addMethodInfo = eventInfo.GetAddMethod(); var addMethodBuilder = methodBuilderFactory(addMethodInfo, isExplicit); eventBuilder.SetAddOnMethod(addMethodBuilder); // Build event remove method. var removeMethodInfo = eventInfo.GetRemoveMethod(true); var removeMethodBuilder = methodBuilderFactory(removeMethodInfo, isExplicit); eventBuilder.SetRemoveOnMethod(removeMethodBuilder); // Build event raise method. var raiseMethodInfo = eventInfo.GetRaiseMethod(true); if (raiseMethodInfo != null) { var methodBuilder = methodBuilderFactory(raiseMethodInfo, isExplicit); eventBuilder.SetRaiseMethod(methodBuilder); } // Build event other methods. var otherMethodInfos = eventInfo.GetOtherMethods(true); // Mono returns null in case no other methods are defined. if (otherMethodInfos != null) { foreach (var otherMethodInfo in otherMethodInfos) { var methodBuilder = methodBuilderFactory(otherMethodInfo, isExplicit); eventBuilder.AddOtherMethod(methodBuilder); } } }