简体   繁体   中英

Automapper Upgrade FindTypeMapFor<>

I'm updating an old .NET Framework application to the latest supported Automapper nuget (from v3.3.1 to v10.0.0). The original code extends automapper in horrendous ways, which I have been able to convert to use built in methods, except this one:

// Ignores all destination properties which would otherwise be mapped by convention.
public static IMappingExpression<TSource, TDestination> IgnoreUnMapped<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
{
    TypeMap map = Mapper.FindTypeMapFor<TSource, TDestination>();

        foreach (var property in map.GetCustomPropertyMaps()
          .Where(p => p.CustomExpression == null)
          .Select(p => p.DestinationProperty.Name))
    {
        expression.ForMember(property, opt => opt.Ignore());
    }
    return expression;
}

I found this page , that gives a workaround for v4.2, but that no longer works for v10.

This requires a cast, but this is the only option that Automapper authors left for people that actually have to maintain software:

    var typeMapConfiguration = (TypeMapConfiguration)expression;

    foreach (var property in typeMapConfiguration.TypeMap.GetUnmappedPropertyNames())
    {
        expression.ForMember(property, opt => opt.Ignore());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM