Exemplo n.º 1
0
        /// <summary>
        /// Registers the specified handler type to the message dispatcher.
        /// </summary>
        /// <param name="messageDispatcher">Message dispatcher instance.</param>
        /// <param name="handlerType">The type to be registered.</param>
        private static void RegisterType(IMessageDispatcher messageDispatcher, Type handlerType)
        {
            MethodInfo methodInfo = messageDispatcher.GetType().GetMethod("Register", new[] { typeof(Type) });

            var handlerIntfTypeQuery = handlerType.GetInterfaces()
                                       .Where(type => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IHandler <>));

            foreach (var handlerIntfType in handlerIntfTypeQuery)
            {
                UnityService.RegisterType(handlerIntfType, handlerType);
                var        messageType       = handlerIntfType.GetGenericArguments().First();
                MethodInfo genericMethodInfo = methodInfo.MakeGenericMethod(messageType);
                genericMethodInfo.Invoke(messageDispatcher, new object[] { handlerIntfType });
            }
        }