In AutoMapper 2.2.1, is there any way I can configure my mappings so that when a property is not explicitly ignored, an exception is thrown? For example, I have the following classes and configuration:
public class Source
{
public int X { get; set; }
public int Y { get; set; }
public int Z { get; set; }
}
public class Destination
{
public int X { get; set; }
public int Y { get; set; }
}
// Config
Mapper.CreateMap<Source, Destination>();
The behavior I receive with this configuration is that the Destination.X
and Destination.Y
properties are set. Furthermore, if I test my configuration:
Mapper.AssertConfigurationIsValid();
Then I will receive no mapping exceptions. What I would like to happen is that an AutoMapperConfigurationException
is thrown because Source.Z
is not explicitly ignored.
I would like it so that I have to explicitly ignore the Z property in order for AssertConfiguartionIsValid
to run without exceptions:
Mapper.CreateMap<Source, Destination>()
.ForSourceMember(m => m.Z, e => e.Ignore());
Currently, AutoMapper does not throw an exception. I would like it to throw an exception if I do not explicitly specify the Ignore
. How can I do this?
Here is method which asserts that all source type properties are mapped:
It checks all configured mappings and verifies that each source type property has mapping defined (either mapped, or ignored).
Usage:
That throws exception
If you will ignore that property, all is fine: