Search code examples
c#inheritancemultiple-inheritanceautomapper-6

ForAllOtherMembers exclude base properties


I'm mapping some properties of class A using AutoMapper 6.2.2, and for all other members, I'm ignoring them with code following:

expression.ForAllOtherMembers(f => f.Ignore());

This is what I want, but it ignores properties that are in the destination A class's base class (properties of BaseA) too. I want to map them (properties from base class) using AutoMapper with mapping A class's some properties. I want function with code like this: 1. map some properties from class A 2. map ALL properties from BaseA (and BaseBaseA, and BaseBaseBaseA, and etc.) 3. ignore all other properties from class A

If someone has any idea, help please. Thanks.

P.S. I'm upgrading AutoMapper from v3.2.1 to v6.2.2. I was using function, that was ignoring all not mapped properties from class A (BaseA properties was mapped normally). After changes in new AutoMapper, I can't use same function - I'm searching alternate ways to do this.


Solution

  • I found the solution, I've achieved this using current classes comparing:

    var destType = typeof(TDestination); expression.ForAllOtherMembers(f => { if (f.DestinationMember.DeclaringType == destType) f.Ignore(); });

    After changing structure of AutoMapper, I've changed a lot in my project. I think, someone who upgrades AutoMapper init's project, would use this code, for case like mine..