// ========================================================================== // ActionContextDispatcherFactory.cs // PinkParrot Headless CMS // ========================================================================== // Copyright (c) PinkParrot Group // All rights reserved. // ========================================================================== using System; using System.Reflection; namespace PinkParrot.Infrastructure.Dispatching { internal class ActionContextDispatcherFactory { public static Tuple> CreateActionHandler(MethodInfo methodInfo) { var inputType = methodInfo.GetParameters()[0].ParameterType; var factoryMethod = typeof(ActionContextDispatcherFactory) .GetMethod("Factory", BindingFlags.Static | BindingFlags.NonPublic) .MakeGenericMethod(typeof(TTarget), inputType, typeof(TContext)); var handler = factoryMethod.Invoke(null, new object[] { methodInfo }); return new Tuple>(inputType, (Action)handler); } // ReSharper disable once UnusedMember.Local private static Action Factory(MethodInfo methodInfo) { var type = typeof(Action); var handler = (Action)methodInfo.CreateDelegate(type); return (target, input, context) => handler(target, (TIn)input, context); } } }