Beispiel #1
0
        internal static 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);
        }
Beispiel #2
0
        internal static bool TryCreateHandlerData(MethodInfo methodInfo, out HandlerMethodData data)
        {
            //No attribute found, handler cannot be created
            if (!(methodInfo.GetCustomAttributes(typeof(HandlerAttribute), true).FirstOrDefault() is HandlerAttribute attribute))
            {
                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);
        }