inquisitive answer works fine, but it can be increased for use in real life, when some mappings are performed from data models in the service model, and virtual members from the source type should be ignored.
In addition, if the type implements some interface, these properties will be displayed as virtual, so the condition !IsFinal must be added to remove these false positive virtual properties.
public static class AutoMapperExtensions { public static IMappingExpression<TSource, TDestination> IgnoreAllDestinationVirtual<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression) { var desType = typeof(TDestination); foreach (var property in desType.GetProperties().Where(p => p.GetGetMethod().IsVirtual && !p.GetGetMethod().IsFinal)) { expression.ForMember(property.Name, opt => opt.Ignore()); } return expression; } public static IMappingExpression<TSource, TDestination> IgnoreAllSourceVirtual<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression) { var srcType = typeof(TSource); foreach (var property in srcType.GetProperties().Where(p => p.GetGetMethod().IsVirtual && !p.GetGetMethod().IsFinal)) { expression.ForSourceMember(property.Name, opt => opt.Ignore()); } return expression; } }
source share