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