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 cl
Here is method which asserts that all source type properties are mapped:
public static void AssertAllSourcePropertiesMapped()
{
foreach (var map in Mapper.GetAllTypeMaps())
{
// Here is hack, because source member mappings are not exposed
Type t = typeof(TypeMap);
var configs = t.GetField("_sourceMemberConfigs", BindingFlags.Instance | BindingFlags.NonPublic);
var mappedSourceProperties = ((IEnumerable<SourceMemberConfig>)configs.GetValue(map)).Select(m => m.SourceMember);
var mappedProperties = map.GetPropertyMaps().Select(m => m.SourceMember)
.Concat(mappedSourceProperties);
var properties = map.SourceType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (var propertyInfo in properties)
{
if (!mappedProperties.Contains(propertyInfo))
throw new Exception(String.Format("Property '{0}' of type '{1}' is not mapped",
propertyInfo, map.SourceType));
}
}
}
It checks all configured mappings and verifies that each source type property has mapping defined (either mapped, or ignored).
Usage:
Mapper.CreateMap<Source, Destination>();
// ...
AssertAllSourcePropertiesMapped();
That throws exception
Property 'Int32 Z' of type 'YourNamespace.Source' is not mapped
If you will ignore that property, all is fine:
Mapper.CreateMap<Source, Destination>()
.ForSourceMember(s => s.Z, opt => opt.Ignore());
AssertAllSourcePropertiesMapped();