2012-02-01 4 views

답변

2

당신이 opt.Skip가 here을 제안하여 시도 가지고

[TestMethod] 
public void TestMethod4() 
{ 
    var a = new A { Nested = new NestedA { V = 1, S = "A" } }; 
    var b = new B { Nested = new NestedB { V = 2, S = string.Empty } }; 

    Mapper.CreateMap<B, A>(); 
    Mapper.CreateMap<NestedB, NestedA>().ForMember(s => s.S, opt => opt.Condition(src => !string.IsNullOrWhiteSpace(src.S))); 
    var result = Mapper.Map(b, a); 

     Assert.AreEqual(2, result.Nested.V);  // OK 
     Assert.AreEqual("A", result.Nested.S);  // FAIL: S == null 
} 

감사 : 임 다음 코드를 시도했다.

Mapper.CreateMap<NestedB, NestedA>() 
.ForMember(s => s.S, opt => opt.Skip(src => !string.IsNullOrWhiteSpace(src.S))); 

편집 : 소스의 뒷조사 후

. TypeMapObjectMapperRegistry 클래스 (중첩 된 객체의 매핑을 처리하는 클래스)에서 UseDestinationValue를 사용하여 대상 값을 보존해야하는지 확인하기 전에 반환한다는 것을 알았습니다. 그렇지 않으면,이 제안 거라고 :

Mapper.CreateMap<B, A>(); 
      Mapper.CreateMap<NestedB, NestedA>() 
       .ForMember(s => s.S, opt => opt.Condition(src => !string.IsNullOrWhiteSpace(src.S))) 
       .ForMember(s => s.S, opt => opt.UseDestinationValue()); 

내가 지미 여기에 핵심 문제를 해결하기 보인다 this을 발견했다.

그래서, 내가 찾은 바에 따르면, Condition과 UseDestinationValue를 동시에 사용하는 방법이없는 것처럼 보입니다.

+0

Im은 AutoMapper v2를 사용하며 건너 뛰기 옵션이 없습니다. – user1183964

관련 문제