Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. using AutoMapper;
  2. using AutoMapper.Attributes;
  3. using System.Reflection;
  4.  
  5. namespace ConsoleApp2
  6. {
  7.     class MapsFromSourceAttribute : MapsFromAttribute
  8.     {
  9.         public MapsFromSourceAttribute() : base(typeof(Source)) { }
  10.  
  11.         public void ConfigureMapping(IMappingExpression<Source, Destination> mappingExpression)
  12.         {
  13.             mappingExpression.ForMember(T => T.BarValue, T => T.MapFrom(W => W.FooValue));
  14.         }
  15.     }
  16.  
  17.     class MapsFromSourceExAttribute : MapsFromAttribute
  18.     {
  19.         public MapsFromSourceExAttribute() : base(typeof(Source)) { }
  20.  
  21.         public void ConfigureMapping(IMappingExpression<Source, DestinationEx> mappingExpression)
  22.         {
  23.             // This >>GETS<< called, but the result is like it doesn't at all!
  24.  
  25.             mappingExpression.ForMember(T => T.BarValue, T => T.MapFrom(W => W.FooValue)); // This property is here again since this is independent mapping.
  26.             mappingExpression.ForMember(T => T.FooBarValue, T => T.MapFrom(W => -W.FooValue)); // New property, but both don't get mapped.
  27.         }
  28.     }
  29.  
  30.     class Source
  31.     {
  32.         public string Property { get; set; }
  33.         public int FooValue { get; set; }
  34.     }
  35.  
  36.     [MapsFromSource] // If this attribute is removed, than the issue is RESOLVED. But we don't want to remove it, they're supposed to work independently.
  37.     class Destination
  38.     {
  39.         public string Property { get; set; }
  40.         public int BarValue { get; set; }
  41.     }
  42.  
  43.     [MapsFromSourceEx]
  44.     class DestinationEx : Destination
  45.     {
  46.         public int FooBarValue { get; set; }
  47.     }
  48.  
  49.     class Program
  50.     {
  51.         static Program()
  52.         {
  53.             Mapper.Initialize(c =>
  54.             {
  55.                 // Manual mapping here is overridden and the issue persists.
  56.  
  57.                 Assembly.GetExecutingAssembly().MapTypes(c);
  58.  
  59.                 // Manual mapping here SOLVES the issue, and the properties are mapped successfully.
  60.             });
  61.         }
  62.  
  63.         static void Main(string[] args)
  64.         {
  65.             Source s = new Source()
  66.             {
  67.                 Property = "stringProp",
  68.                 FooValue = 1
  69.             };
  70.  
  71.             // d's properties BarValue and FooBarValue are not mapped!
  72.             DestinationEx d = Mapper.Map<DestinationEx>(s);
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement