Exemplo n.º 1
0
        internal IHandler CreateHandler(HandlerMethodData handlerData, object eventProxy)
        {
            var paramType = handlerData.ParameterType;


            //Creates Handler<TEvent> type using TEvent from method parameter type
            var handlerType = typeof(Handler <>).MakeGenericType(paramType);


            //Creates Action<T> type using T from method parameter type
            var actionType = typeof(Action <>).MakeGenericType(paramType);


            //Creates instance of Action<T> using T from method parameter type
            var action = Delegate.CreateDelegate(actionType, eventProxy, handlerData.Method);


            //Creates instance of Handler<TEvent> using TEvent param type , handler priority from attribute and action from Methodinfo
            var handler = Activator.CreateInstance(handlerType, paramType, handlerData.AttachedAttribute.Priority, action);

            return(handler as IHandler);
        }
Exemplo n.º 2
0
        internal bool TryCreateHandlerData(MethodInfo methodInfo, out HandlerMethodData data)
        {
            var attribute = methodInfo.GetCustomAttributes(typeof(HandlerAttribute), true).FirstOrDefault() as HandlerAttribute;


            //No attribute found, handler cannot be created
            if (attribute == null)
            {
                data = null;
                return(false);
            }

            var methodParams = methodInfo.GetParameters();

            //Method doesn't mach handler pattern, handler cannot be created
            if (methodParams.Length != 1)
            {
                data = null;
                return(false);
            }


            var methodParameter = methodParams.FirstOrDefault();

            var paramType = methodParameter?.ParameterType;

            var handlerData = new HandlerMethodData()
            {
                AttachedAttribute = attribute,

                Method = methodInfo,

                ParameterType = paramType
            };

            data = handlerData;

            return(true);
        }