private static TTarget ThrowCtor <TSource, TTarget>(TSource source, MapperContext context) => throw new InvalidOperationException($"Don't know how to create {typeof(TTarget).FullName} instances.");
private static void Identity <TSource, TTarget>(TSource source, TTarget target, MapperContext context) { }
private TTarget Map <TTarget>(object source, Type sourceType, MapperContext context) { if (source == null) { return(default);
/// <summary> /// Maps a source object to a new target object. /// </summary> /// <typeparam name="TSource">The source type.</typeparam> /// <typeparam name="TTarget">The target type.</typeparam> /// <param name="source">The source object.</param> /// <param name="context">A mapper context.</param> /// <returns>The target object.</returns> public TTarget Map <TSource, TTarget>(TSource source, MapperContext context) => Map <TTarget>(source, typeof(TSource), context);
/// <summary> /// Maps a source object to a new target object. /// </summary> /// <typeparam name="TTarget">The target type.</typeparam> /// <param name="source">The source object.</param> /// <param name="context">A mapper context.</param> /// <returns>The target object.</returns> public TTarget Map <TTarget>(object source, MapperContext context) => Map <TTarget>(source, source?.GetType(), context);
/// <summary> /// Maps an enumerable of source objects to a new list of target objects. /// </summary> /// <typeparam name="TSourceElement">The type of the source objects.</typeparam> /// <typeparam name="TTargetElement">The type of the target objects.</typeparam> /// <param name="source">The source objects.</param> /// <param name="context">A mapper context.</param> /// <returns>A list containing the target objects.</returns> public List <TTargetElement> MapEnumerable <TSourceElement, TTargetElement>(IEnumerable <TSourceElement> source, MapperContext context) { return(source.Select(x => Map <TSourceElement, TTargetElement>(x, context)).ToList()); }
private TTarget MapEnumerableInternal <TTarget>(IEnumerable source, Type targetGenericArg, Func <object, MapperContext, object> ctor, Action <object, object, MapperContext> map, MapperContext context) { var targetList = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(targetGenericArg)); foreach (var sourceItem in source) { var targetItem = ctor(sourceItem, context); map(sourceItem, targetItem, context); targetList.Add(targetItem); } object target = targetList; if (typeof(TTarget).IsArray) { var elementType = typeof(TTarget).GetElementType(); if (elementType == null) { throw new Exception("panic"); } var targetArray = Array.CreateInstance(elementType, targetList.Count); targetList.CopyTo(targetArray, 0); target = targetArray; } return((TTarget)target); }