2016-09-09 5 views
1

Hier ist mein Problem, in Condition Ich möchte den Namen der aktuellen Eigenschaft erhalten, die ausgewertet wird. Ich glaube, dass Sie dies in früheren Versionen von Automapper tun könnten. Irgendwelche Vorschläge?Automapper (5.1.1) ForAllMembers - Name der aktuellen Eigenschaft

[TestFixture] 
public class SandBox 
{ 
    public class MySource 
    { 
     public string Name { get; set; } 
     public int Count { get; set; } 
    } 

    public class MyDestination 
    { 
     public string Name { get; set; } 
     public int Count { get; set; } 
    } 

    public class SourceProfile : Profile 
    { 
     public SourceProfile() 
     { 
      this.CreateMap<MySource, MyDestination>() 
       .ForAllMembers(x => x.Condition((source, destination, arg3, arg4, resolutionContext) => 
       { 
        // this will run twice (once for every property) 
        // but how can I find out, what the current property is? 

        return true; 
       })); 
     } 
    } 

    public SandBox() 
    { 
     Mapper.Initialize(x => 
     { 
      x.AddProfile(new SourceProfile()); 
     }); 
    } 

    [Test] 
    public void Run() 
    { 
     var s = new MySource { Name = "X", Count = 42 }; 
     var r = Mapper.Map<MyDestination>(s); 
     Assert.AreEqual(s.Name, r.Name); 
    } 
} 

Antwort

2

Versuchen Sie Folgendes:

this.CreateMap<MySource, MyDestination>() 
      .ForAllMembers(x => x.Condition((source, destination, arg3, arg4, resolutionContext) => 
      { 
       // this will run twice (once for every property) 
       // but how can I find out, what the current property is? 

       Debug.WriteLine($"Mapping to {destination.GetType().Name}.{x.DestinationMember.Name}"); 

       return true; 
      })); 
+0

Arbeitete groß, danke! – Pelle

Verwandte Themen