2016-09-09 3 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

관련 문제