简体   繁体   中英

Automapper Aftermap "method not found" exception

I'm trying to use the AfterMap method to flatten the input class to output. The input classes look as following:

public class FooDTO {
  public string SomeFieldA { get; set; }
  public string SomeFieldB { get; set; }
  ....
  public BarDTO SomeFieldY { get; set; }
  public BazDTO SomeFieldZ { get; set; }
}

public class BarDTO {
  public string SomeOtherFieldA { get; set; }
}

public class BazDTO {
  public string YetAnotherFieldA { get; set; }
}

and the output class as following:

public class Foo {
  public string SomeFieldA { get; set; }
  public string SomeFieldB { get; set; }
  ....      
  public string SomeOtherFieldA { get; set; }
  public string YetAnotherFieldA { get; set; }
}

My AutoMapper mapping configuration looks as following:

CreateMap<FooDTO, Foo>()
   .AfterMap((src, dest, ctx) => ctx.Mapper.Map(src.SomeFieldY, dest))
   .AfterMap((src, dest, ctx) => ctx.Mapper.Map(src.SomeFieldZ, dest));
CreateMap<BarDTO, Foo>();
CreateMap<BazDTO, Foo>();

My problem is that when I try to run my application, I get the following unhandled exception:

 System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.MissingMethodException: Method not found: 'AutoMapper.IMappingExpression`2<!0,!1> AutoMapper.IMappingExpression`2.AfterMap(System.Action`3<!0,!1,AutoMapper.ResolutionContext>)'.

Am I missing some other configuration? How do I make my code work?

As Lucian Bargaoanu mentioned, it was caused by mismatch of versions of Nuget packages. Once I upgraded another project to version 9.0.0, it started to work.

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