2013-11-04 71 views
2

有沒有辦法告訴AutoMapper跳過所有空屬性(因爲我的對象有50個屬性),然後允許某些屬性爲空後綴?AutoMapper跳過所有空屬性,然後允許一些空值

換句話說,我寧願不「白名單」49應該跳過的屬性。我寧願將它們全部默認爲跳過,然後將其列入「白名單」以允許空值。

第一部分當然可能看起來像我想象的那樣。

Mapper.CreateMap<MyClassA, MyClassB>()     
.ForAllMembers(opt => opt.Condition(srs => !srs.IsSourceValueNull)); 

回答

3

您可以使用AfterMap某些屬性手動映射,並跳過所有其他空的屬性:

Mapper.CreateMap<MyClassA, MyClassB>() 
     .AfterMap((a,b) => b.Foo = a.Foo) // will be mapped if Foo is null 
     .ForAllMembers(opt => opt.Condition(srs => !srs.IsSourceValueNull)); 
+1

非常感謝!這是100%的答案,超級簡單! –