CQRSalad.Dispatching.Context.DispatcherExecutorsManager.CreateExecutorDelegate C# (CSharp) Method

CreateExecutorDelegate() private static method

private static CreateExecutorDelegate ( Type handlerType, MethodInfo action, Type messageType ) : HandlerExecutor
handlerType System.Type
action System.Reflection.MethodInfo
messageType System.Type
return HandlerExecutor
        private static HandlerExecutor CreateExecutorDelegate(Type handlerType, MethodInfo action, Type messageType)
        {
            Type objectType = typeof(object);
            ParameterExpression handlerParameter = Expression.Parameter(objectType, "handler");
            ParameterExpression messageParameter = Expression.Parameter(objectType, "message");

            MethodCallExpression methodCall =
                Expression.Call(
                    Expression.Convert(handlerParameter, handlerType),
                    action,
                    Expression.Convert(messageParameter, messageType));

            if (action.ReturnType == typeof(void))
            {
                var lambda = Expression.Lambda<Action<object, object>>(
                    methodCall,
                    handlerParameter,
                    messageParameter);

                Action<object, object> voidExecutor = lambda.Compile();
                return (handler, message) =>
                {
                    voidExecutor(handler, message);
                    return null;
                };
            }
            else
            {
                var lambda = Expression.Lambda<HandlerExecutor>(
                    Expression.Convert(methodCall, typeof(object)),
                    handlerParameter,
                    messageParameter);

                return lambda.Compile();
            }
        }
    }