2012-04-04 113 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