private static GenericHandlerDelegate GetHandlerMethod(IExportEvent exportEvent) { return(HandlerMethodMap.GetOrAdd(exportEvent.GetType(), type => { var payloadType = GetPayloadType(type); if (payloadType == null) { return null; } var handlersType = typeof(IEnumerable <>).MakeGenericType( typeof(IExportEventHandler <>).MakeGenericType(payloadType) ); var eventType = typeof(IExportEvent <>).MakeGenericType(payloadType); var method = HandleGenericEventMethod.MakeGenericMethod(payloadType); var handlersParam = Expression.Parameter(typeof(object), "handlers"); var eventParam = Expression.Parameter(typeof(IExportEvent), "exportEvent"); var tokenParam = Expression.Parameter(typeof(CancellationToken), "token"); var castHandlers = Expression.Convert(handlersParam, handlersType); var castEvent = Expression.Convert(eventParam, eventType); var callExpression = Expression.Call(method, castHandlers, castEvent, tokenParam); var lambda = Expression.Lambda <GenericHandlerDelegate>(callExpression, handlersParam, eventParam, tokenParam); return lambda.Compile(); })); }
private static Type GetHandlerType(IExportEvent exportEvent) { return(EventHandlerTypeMap.GetOrAdd(exportEvent.GetType(), type => { var payloadType = GetPayloadType(type); if (payloadType == null) { return null; } var handlerType = typeof(IExportEventHandler <>).MakeGenericType(payloadType); var enumerableType = typeof(IEnumerable <>).MakeGenericType(handlerType); return enumerableType; })); }