public static IServiceCollection AddMemoryEventBus(this IServiceCollection services, Assembly[] handlerAssemblies) { services.TryAddSingleton <MemoryEventBusStarter>(); services.TryAddSingleton <IEventPublish, MemoryEventPublish>(); ConcurrentDictionary <string, Type> eventHandlerMap = new ConcurrentDictionary <string, Type>(); foreach (var handlerType in handlerAssemblies.SelectMany(assembly => assembly.GetTypes().Where(type => type.IsClass && typeof(IEventHandler).IsAssignableFrom(type)))) { EventHandlerAttribute eventHandlerAttribute = handlerType.GetCustomAttribute <EventHandlerAttribute>(); if (eventHandlerAttribute == null) { throw new InvalidOperationException($"The type {handlerType.FullName} should have {nameof(EventHandlerAttribute)} attribute"); } if (!eventHandlerMap.TryAdd(eventHandlerAttribute.ThrowIfNull(nameof(eventHandlerAttribute)).EventName, handlerType)) { throw new InvalidOperationException($"There are duplicate handlers for one event name {eventHandlerAttribute.EventName}"); } services.Add(new ServiceDescriptor(handlerType, handlerType, eventHandlerAttribute.HandlerLifetime)); } services.AddSingleton(sp => new MemoryEventDispatcher(sp, sp.GetRequiredService <ILogger <MemoryEventDispatcher> >(), eventHandlerMap)); return(services); }
protected override void AddEventHandler(TestElement?element, EventHandlerAttribute eventHandlerAttribute, Func <Type?, Delegate?> handlerCreator, EventHandlerBase <TestElement, EventHandlerItemTss> eventHandlers) { eventHandlers.Add(new EventHandlerItemTss( eventHandlerAttribute.ElementName, element, eventHandlerAttribute.Event, handlerCreator(null), eventHandlerAttribute.HandledEventsToo )); }
protected override void AddEventHandler(FrameworkElement element, EventHandlerAttribute eventHandlerAttribute, Func <Type, Delegate> handlerCreator, EventHandlerBase <FrameworkElement, UwpEventHandlerItem> eventHandlers) { var targetElement = element.FindElement <FrameworkElement>(eventHandlerAttribute.ElementName); var routedEvent = RetrieveRoutedEvent(targetElement, eventHandlerAttribute.Event); var eventInfo = RetrieveEventInfo(targetElement, eventHandlerAttribute.Event); eventHandlers.Add(new UwpEventHandlerItem( eventHandlerAttribute.ElementName, targetElement, eventHandlerAttribute.Event, routedEvent, eventInfo, handlerCreator(eventInfo?.EventHandlerType), eventHandlerAttribute.HandledEventsToo )); }
protected override void AddEventHandler(StyledElement element, EventHandlerAttribute eventHandlerAttribute, Func <Type, Delegate> handlerCreator, EventHandlerBase <StyledElement, AvaloniaEventHandlerItem> eventHandlers) { var targetElement = element.FindElement <StyledElement>(eventHandlerAttribute.ElementName); var routedEvent = RetrieveRoutedEvent(targetElement, eventHandlerAttribute.Event); var eventInfo = RetrieveEventInfo(element, eventHandlerAttribute.Event); eventHandlers.Add(new AvaloniaEventHandlerItem( eventHandlerAttribute.ElementName, targetElement, eventHandlerAttribute.Event, routedEvent, eventInfo, handlerCreator(routedEvent?.EventArgsType == null ? eventInfo?.EventHandlerType : typeof(EventHandler <>).MakeGenericType(routedEvent.EventArgsType)), eventHandlerAttribute.HandledEventsToo )); }
internal static bool AssureValidity(MethodInfo method, EventHandlerAttribute attr) { if (!method.IsGenericMethod && !method.IsGenericMethodDefinition && method.ReturnType == typeof(void)) { var methodParams = method.GetParameters(); if (methodParams.Length == 1 && methodParams[0].ParameterType == typeof(Event)) { return(true); } else { Debug.LogWarning("Method " + method.Name + " has incorrect signature for EventHandlerAttribute!"); } } return(false); }
public static void SetupInput() { _eventHandlers = new List <KeyValuePair <EventHandlerAttribute, Delegate> > (); MenuItems = new List <KeyValuePair <MenuItemAttribute, Delegate> > (); var scriptAssemblies = AppDomain.CurrentDomain.GetAssemblies().Where((Assembly assembly) => assembly.FullName.Contains("Assembly")); foreach (var assembly in scriptAssemblies) { foreach (var type in assembly.GetTypes()) { foreach (var method in type.GetMethods(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)) { Delegate actionDelegate = null; foreach (var attr in method.GetCustomAttributes(true)) { var attrType = attr.GetType(); if (attrType == typeof(EventHandlerAttribute)) { if (EventHandlerAttribute.AssureValidity(method, attr as EventHandlerAttribute)) { if (actionDelegate == null) { actionDelegate = Delegate.CreateDelegate(typeof(Action <Event>), method); } _eventHandlers.Add(new KeyValuePair <EventHandlerAttribute, Delegate>(attr as EventHandlerAttribute, actionDelegate)); } } if (attrType == typeof(MenuItemAttribute)) { if (actionDelegate == null) { actionDelegate = Delegate.CreateDelegate(typeof(Action), method); } MenuItems.Add(new KeyValuePair <MenuItemAttribute, Delegate>(attr as MenuItemAttribute, actionDelegate)); } } } } } _eventHandlers.Sort((handlerA, handlerB) => handlerA.Key.Priority.CompareTo(handlerB.Key.Priority)); }
internal EventHandlerData(MethodInfo method, object instance) { _event = (DelEventHandler)Delegate.CreateDelegate(typeof(DelEventHandler), instance, method, true); _attribute = method.GetCustomAttribute <EventHandlerAttribute>(); }
/// <summary> /// Adds an event handler to the specified <see cref="EventHandlerBase{TElement,TItem}"/> using the specified element, /// <see cref="EventHandlerAttribute"/>, and the function to create a delegate of the event handler. /// </summary> /// <param name="element">The element that raises the event.</param> /// <param name="eventHandlerAttribute">The <see cref="EventHandlerAttribute"/> specified to the method that handles the event.</param> /// <param name="handlerCreator">The function to create a delegate of the event handler.</param> /// <param name="eventHandlers">The <see cref="EventHandlerBase{TElement,TItem}"/> to add the event handler.</param> protected abstract void AddEventHandler(TElement?element, EventHandlerAttribute eventHandlerAttribute, Func <Type?, Delegate?> handlerCreator, EventHandlerBase <TElement, TItem> eventHandlers);
public static void SetupInput(List <Type> setType) { eventHandlers = new List <KeyValuePair <EventHandlerAttribute, Delegate> >(); hotkeyHandlers = new List <KeyValuePair <HotkeyAttribute, Delegate> >(); contextEntries = new List <KeyValuePair <ContextEntryAttribute, MenuFunctionData> >(); IEnumerable <Assembly> scriptAssemblies = AppDomain.CurrentDomain.GetAssemblies().Where((Assembly assembly) => assembly.FullName.Contains("Assembly")); foreach (Assembly assembly in scriptAssemblies) { foreach (Type type in assembly.GetTypes()) { bool getType = false; foreach (var t in setType) { if (type == t || type == typeof(EditorState)) { getType = true; break; } } if (!getType) { continue; } foreach (MethodInfo method in type.GetMethods(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)) { Delegate actionDelegate = null; foreach (object attr in method.GetCustomAttributes(true)) { Type attrType = attr.GetType(); if (attrType == typeof(EventHandlerAttribute)) { if (EventHandlerAttribute.AssureValidity(method, attr as EventHandlerAttribute)) { if (actionDelegate == null) { actionDelegate = Delegate.CreateDelegate(typeof(Action <EditorInputInfo>), method); } eventHandlers.Add(new KeyValuePair <EventHandlerAttribute, Delegate>(attr as EventHandlerAttribute, actionDelegate)); } } else if (attrType == typeof(HotkeyAttribute)) { if (HotkeyAttribute.AssureValidity(method, attr as HotkeyAttribute)) { if (actionDelegate == null) { actionDelegate = Delegate.CreateDelegate(typeof(Action <EditorInputInfo>), method); } hotkeyHandlers.Add(new KeyValuePair <HotkeyAttribute, Delegate>(attr as HotkeyAttribute, actionDelegate)); } } else if (attrType == typeof(ContextEntryAttribute)) { if (ContextEntryAttribute.AssureValidity(method, attr as ContextEntryAttribute)) { if (actionDelegate == null) { actionDelegate = Delegate.CreateDelegate(typeof(Action <EditorInputInfo>), method); } MenuFunctionData menuFunction = (object callbackObj) => { if (!(callbackObj is EditorInputInfo)) { throw new UnityException("Callback Object passed by context is not of type EditorMenuCallback!"); } actionDelegate.DynamicInvoke(callbackObj as EditorInputInfo); }; contextEntries.Add(new KeyValuePair <ContextEntryAttribute, MenuFunctionData>(attr as ContextEntryAttribute, menuFunction)); } } } } } } eventHandlers.Sort((handlerA, handlerB) => handlerA.Key.priority.CompareTo(handlerB.Key.priority)); hotkeyHandlers.Sort((handlerA, handlerB) => handlerA.Key.priority.CompareTo(handlerB.Key.priority)); }