Advertisement
Guest User

Untitled

a guest
Sep 25th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.89 KB | None | 0 0
  1. namespace CarDealer.Tests.Helpers
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     using AutoMapper;
  6.     using Common.AutoMapping.Interfaces;
  7.  
  8.     public class TestProfile : Profile
  9.     {
  10.         public TestProfile()
  11.         {
  12.             this.ConfigureProfile();
  13.         }
  14.  
  15.         private void ConfigureProfile()
  16.         {
  17.             var allTypes = AppDomain
  18.                 .CurrentDomain
  19.                 .GetAssemblies()
  20.                 .Where(a => a.GetName().FullName.Contains(nameof(CarDealer)))
  21.                 .SelectMany(a=> a.GetTypes());
  22.  
  23.            
  24.             var allMappingTypes = allTypes
  25.                 .Where(t => t.IsClass
  26.                             && !t.IsAbstract
  27.                             && t.GetInterfaces()
  28.                                 .Where(i => i.IsGenericType)
  29.                                 .Select(i => i.GetGenericTypeDefinition())
  30.                                 .Contains(typeof(IMapWith<>)))
  31.                 .Select(t => new
  32.                 {
  33.                     Destination = t,
  34.                     Sourse = t.GetInterfaces()
  35.                         .Where(i => i.IsGenericType)
  36.                         .Select(i => new
  37.                         {
  38.                             Definition = i.GetGenericTypeDefinition(),
  39.                             Arguments = i.GetGenericArguments()
  40.                         })
  41.                         .Where(i => i.Definition == typeof(IMapWith<>))
  42.                         .SelectMany(i => i.Arguments)
  43.                         .First()
  44.                 })
  45.                 .ToList();
  46.  
  47.             //Creates bidirectional mapping for all types, which extends IMapWith<TModel> interface
  48.             foreach (var type in allMappingTypes)
  49.             {
  50.                 this.CreateMap(type.Destination, type.Sourse);
  51.                 this.CreateMap(type.Sourse, type.Destination);
  52.             }
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement