Search code examples
c#.netautomapperautomapper-5

Migrating to AutoMapper 5


I was using a AutoMapper 4.X as following (simplified snippet)

Mapper.CreateMap<A,B>()
  .ForMember(myB.MyProp, o => o.Foo()); // Foo is an extention method -> see below


public static void Foo<T> (this IMemberConfigurationExpression<T> config)
{
   config.ResolveUsing((resolutionResult, source) =>
   {
          var name = resolutionResult.Context.MemberName; // this is what I want
   }
}

AutoMapper 5.X does not have a resolutionResult anymore when calling config.ResolveUsing so I'm not able to get the information that I want (MemberName) out of it.

Any Idea how to adapt the code to make it work with AutoMapper 5 ?


Solution

  • If you'll cast to MemberConfigurationExpression, you'll have access to its DestinationMember that contains the information you want:

    public static void Foo<TSrc, TObj>(this IMemberConfigurationExpression<TSrc, TObj, object> config)
    {
        config.ResolveUsing((resolutionResult, source) =>
        {
            var memberConfExpr = config as MemberConfigurationExpression<TSrc, TObj, object>;
    
            if (memberConfExpr == null)
                return false;
    
            var name = memberConfExpr.DestinationMember.Name;
    
            // ...
        });
    }