2016-09-09 14 views
1

ここに私の問題がありますCondition評価中の現在のプロパティの名前に行きたいと思います。私はAutomapperの以前のバージョンでこれを行うことができると信じています。助言がありますか?Automapper(5.1.1)ForAllMembers - 現在のプロパティの名前を取得

[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); 
    } 
} 

答えて

2

は、以下のことを試してみてください。

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

は素晴らしい仕事、ありがとう! – Pelle

関連する問題