2012-04-04 44 views
2

試圖根據viewModel上的特定字段跳過屬性映射。有什麼辦法來訪問ForAllMembers源對象 - >工況法ForAllMembers方法中的訪問源對象

Mapper.CreateMap<AViewModel, AEntity>() 
     .IgnoreMembers(ignoreMembers) 
     .ForAllMembers(o => { 
      o.Condition(ctx => { 
         //Need to access AViewModel instance here 
       return "Id" == ctx.MemberName; 
    }); 
    }); 

回答

1

我不知道官方的方式,但如果你是,你可以使用Parent財產上ResolutionContext

Mapper.CreateMap<AViewModel, AEntity>() 
     .IgnoreMembers(ignoreMembers) 
     .ForAllMembers(o => { 
      o.Condition(ctx => { 
       AViewModel instance = (AViewModel)ctx.Parent.SourceValue; 
       return "Id" == ctx.MemberName; 
    }); 
    }); 

在映射的多個層次中,您可以「遍歷」Parent關係,直到找到所需的類型。

+0

我認爲這明確回答了這個問題,但對付你是在對象層次結構中的不確定性會讓我想避免做這種方式。 – 2012-04-04 11:58:10

+0

感謝它的工作 – user1312702 2012-04-04 12:01:50