Ignore all non exisisting properties with Automapper

Code Snippet
public static class MappingCustomExtension
    public static IMappingExpression<TSource, TDestination> IgnoreAllNonExisting<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
        var sourceType = typeof(TSource);
        var destinationType = typeof(TDestination);
        var existingMaps = Mapper.GetAllTypeMaps().First(x => x.SourceType.Equals(sourceType) && x.DestinationType.Equals(destinationType));
        foreach (var property in existingMaps.GetUnmappedPropertyNames())
            expression.ForMember(property, opt => opt.Ignore());
        return expression;


No Comments

You can leave the first : )

Leave a Reply

Your email address will not be published. Required fields are marked *