public static IMessageDispatcher Create(IHandlerProvider handlerProvider, string dispatcherName, params object[] args) { IMessageDispatcher messageDispatcher = ServiceLocator.Instance.GetService<IMessageDispatcher>(dispatcherName); IEnumerable handlers = handlerProvider.GetHandlers(); MethodInfo methodInfo = messageDispatcher.GetType().GetMethod("Register", BindingFlags.Public | BindingFlags.Instance); foreach (object handlerObject in handlers) { var handlerInterfaceTypeQuery = from p in handlerObject.GetType().GetInterfaces() where p.IsGenericType && p.GetGenericTypeDefinition().Equals(typeof(IHandler<>)) select p; if (handlerInterfaceTypeQuery != null) { foreach (var handlerInterfaceType in handlerInterfaceTypeQuery) { Type messageType = handlerInterfaceType.GetGenericTypeDefinition(); MethodInfo genericRegisterMethod = methodInfo.MakeGenericMethod(messageType); genericRegisterMethod.Invoke(messageDispatcher, new object[] { handlerObject }); } } } return messageDispatcher; }
public static IMessageDispatcher Create(IHandlerProvider handlerProvider, string dispatcherName, params object[] args) { IMessageDispatcher messageDispatcher = ServiceLocator.Instance.GetService <IMessageDispatcher>(dispatcherName); IEnumerable handlers = handlerProvider.GetHandlers(); MethodInfo methodInfo = messageDispatcher.GetType().GetMethod("Register", BindingFlags.Public | BindingFlags.Instance); foreach (object handlerObject in handlers) { var handlerInterfaceTypeQuery = from p in handlerObject.GetType().GetInterfaces() where p.IsGenericType && p.GetGenericTypeDefinition().Equals(typeof(IHandler <>)) select p; if (handlerInterfaceTypeQuery != null) { foreach (var handlerInterfaceType in handlerInterfaceTypeQuery) { Type messageType = handlerInterfaceType.GetGenericTypeDefinition(); MethodInfo genericRegisterMethod = methodInfo.MakeGenericMethod(messageType); genericRegisterMethod.Invoke(messageDispatcher, new object[] { handlerObject }); } } } return(messageDispatcher); }
public async Task RouteToHandlers(object messageObject) { var messageType = messageObject.GetType(); Type[] typeArgs = { messageType }; var messageHandler = typeof(IHandle <>).MakeGenericType(typeArgs); MethodInfo method = messageHandler.GetMethods().SingleOrDefault(m => ForHandleMethodForType(m, messageType)); if (method != null) { var list = _handlers.GetHandlers(messageHandler, messageObject); foreach (var handler in list) { await InvokeHandle(handler, method, messageObject); } } }
/// <summary> /// 执行消息 /// </summary> public void Execute(TMessage message) { var messageType = message.GetType(); this.TryExecute(messageType, message, _handlerProvider.GetHandlers(messageType), _logger); }