How to tell Automapper to check if all source properties have destination properties

We have two classes:

public class Foo { public int A { get; set; } public int B { get; set; } public int C { get; set; } } public class Bar { public int A { get; set; } public int B { get; set; } } 

and display configuration

  Mapper.CreateMap<Foo, Bar>; 

Is there any way for Automapper to automatically check that all source properties have the appropriate destination properties, in my example, it throws an exception that notifies us of the Foo.C property that is not mapped to anything. Mapper.AssertConfigurationIsValid () only checks the other way around: all destination properties have source properties, so in my case this does not help.

+4
source share
1 answer

Perhaps you can use the hack and check the display in the other direction. Sort of:

 Mapper.CreateMap<Bar, Foo>; // Swap the direction of the mapping Mapper.AssertConfigurationIsValid() 

I know this is not perfect, but can be a quick fix.

+2
source

Source: https://habr.com/ru/post/1480109/


All Articles