private IImmutableList <IItem> TransformByMap(object content) { if (content == null) { return(ImmutableList <IItem> .Empty); } Type contentType = content.GetType(); MethodInfo serviceMethod = GetType().GetMethod(nameof(GetMappingService)); if (serviceMethod == null) { return(ImmutableList <IItem> .Empty); } MethodInfo genericServiceMethod = serviceMethod.MakeGenericMethod(contentType); IItemMappingService mappingService = (IItemMappingService)genericServiceMethod.Invoke(this, null); if (mappingService == null) { logger.Warn($"No mapping service for result content of type: {contentType.Name}.", contentType, content); return(ImmutableList <IItem> .Empty); } Type genericMappingServiceType = typeof(IItemMappingService <>); Type targetMappingServiceType = genericMappingServiceType.MakeGenericType(contentType); MethodInfo targetMethod = targetMappingServiceType.GetMethod(nameof(IItemMappingService <string> .Map)); if (targetMethod == null) { return(ImmutableList <IItem> .Empty); } try { object items = targetMethod.Invoke(mappingService, new[] { content }); return((IImmutableList <IItem>)items); } catch (Exception e) { logger.Error("Error during mapping from result content to items.", e); return(ImmutableList <IItem> .Empty); } }
public void RegisterMappingService <TContent>(IItemMappingService <TContent> service) { mapping[typeof(TContent)] = service; }