public bool RegisterEventHandler(EventHandlerAttribute attribute, IEventHandler parent, MethodInfo method) { Items[attribute.Priority].Add(new Item(attribute, parent, method)); return(true); //return Items.Add(new Item(attribute, parent, method)); /*if (!EventHandlers.TryAdd(parent, method)) * { * return true; * } * return false;*/ }
/// <summary> /// Registers all EventHandler methods with the current EventDispatcher. /// </summary> /// <param name="obj">The class to scan for EventHandlers</param> public void RegisterEvents(IEventHandler obj) { int count = 0; var type = typeof(Event); Type objType = obj.GetType(); foreach (var method in objType.GetMethods(BindingFlags.Public | BindingFlags.Instance)) { EventHandlerAttribute attribute = method.GetCustomAttribute <EventHandlerAttribute>(false); if (attribute == null) { continue; } var parameters = method.GetParameters(); if (parameters.Length != 1 || !type.IsAssignableFrom(parameters[0].ParameterType)) { continue; } var paramType = parameters[0].ParameterType; EventDispatcherValues e = null; if (!RegisteredEvents.TryGetValue(paramType, out e)) { if (EventTypes.TryAdd(paramType)) { e = new EventDispatcherValues(); RegisteredEvents.Add(paramType, e); } } if (!e.RegisterEventHandler(attribute, obj, method)) { Log.Warn($"Duplicate found for class \"{obj.GetType()}\" of type \"{paramType}\""); } else { count++; } } Log.Info($"Registered {count} event handlers for \"{obj}\""); }
public Item(EventHandlerAttribute attribute, IEventHandler parent, MethodInfo method) { Attribute = attribute; Parent = parent; Method = method; }