Exemple #1
0
        public EventHandlerProvider(Type type, EventHandlerActionDescriptor actionDescriptor)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            _type             = type;
            _actionDescriptor = actionDescriptor;
        }
Exemple #2
0
        public EventHandlerInvoker(object handler, EventHandlerActionDescriptor actionDescriptor, IServiceProvider serviceProvider)
        {
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            _handler          = handler;
            _actionDescriptor = actionDescriptor;
            _serviceProvider  = serviceProvider;
        }
Exemple #3
0
        private bool TryGetHandlingMember(MethodInfo member, out EventHandlerActionDescriptor result)
        {
            var parameters = member.GetParameters();

            if (parameters.Length == 0)
            {
                result = default;
                return(false);
            }

            if (parameters.Any(p => p.ParameterType.IsByRef))
            {
                result = default;
                return(false);
            }

            if (member.IsGenericMethod || member.IsGenericMethodDefinition)
            {
                result = default;
                return(false);
            }

            if (member.IsDefined <NoEventHandlerActionAttribute>())
            {
                result = default;
                return(false);
            }

            var eventType = parameters[0].ParameterType;

            var actionAttribute = member.GetCustomAttribute <EventHandlerActionAttribute>();

            if (actionAttribute != null && actionAttribute.EventType != null)
            {
                if (!eventType.IsAssignableFrom(actionAttribute.EventType))
                {
                    throw new InvalidOperationException();
                }

                eventType = actionAttribute.EventType;
            }

            // Synchronous handler
            if ((member.Name == "Handle" || actionAttribute != null) &&
                (member.ReturnType == typeof(void) || !typeof(Task).IsAssignableFrom(member.ReturnType)))
            {
                result = new EventHandlerActionDescriptor(eventType, member);
                return(true);
            }

            // Asynchronous handler
            if ((member.Name == "HandleAsync" || actionAttribute != null) &&
                (typeof(Task).IsAssignableFrom(member.ReturnType)))
            {
                result = new EventHandlerActionDescriptor(eventType, member);
                return(true);
            }

            result = default;
            return(false);
        }