Headless CMS and Content Managment Hub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

39 lines
1.3 KiB

// ==========================================================================
// FuncDispatcher.cs
// PinkParrot Headless CMS
// ==========================================================================
// Copyright (c) PinkParrot Group
// All rights reserved.
// ==========================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace PinkParrot.Infrastructure.Dispatching
{
public sealed class FuncDispatcher<TTarget, TIn, TOut>
{
private static readonly Dictionary<Type, Func<TTarget, object, TOut>> Handlers;
static FuncDispatcher()
{
Handlers =
typeof(TTarget)
.GetMethods()
.Where(Helper.HasRightName)
.Where(Helper.HasRightParameters<TIn>)
.Where(Helper.HasRightReturnType<TOut>)
.Select(FuncDispatcherFactory.CreateFuncHandler<TTarget, TOut>)
.ToDictionary(h => h.Item1, h => h.Item2);
}
public static TOut Dispatch(TTarget target, TIn item)
{
Func<TTarget, object, TOut> handler;
return Handlers.TryGetValue(item.GetType(), out handler) ? handler(target, item) : default(TOut);
}
}
}