2016-11-15 119 views
2

我使用的是AutoMapper 4.X到AutoMapper如下(簡單的代碼片段)調用config.ResolveUsing遷移5

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沒有resolutionResult了,所以我不能夠得到我想要的信息(MemberName)。

任何想法如何調整代碼以使其與AutoMapper 5一起使用?

回答

2

如果你投以MemberConfigurationExpression,你將有機會獲得其DestinationMember包含你想要的信息:

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; 

     // ... 
    }); 
}