【问题标题】:Automapper Aftermap "method not found" exceptionAutomapper Aftermap“找不到方法”异常
【发布时间】:2022-05-23 06:40:26
【问题描述】:

我正在尝试使用AfterMap 方法将输入类展平为输出。输入类如下所示:

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; }
}

输出类如下:

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

我的 AutoMapper 映射配置如下:

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>();

我的问题是,当我尝试运行我的应用程序时,我得到了以下未处理的异常:

 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 版本不匹配。您必须确保整个应用程序只有一个版本。
  • @LucianBargaoanu,你的意思是 Nuget 包版本吗?

标签: c# automapper


【解决方案1】:

正如 Lucian Bargaoanu 所说,这是由 Nuget 包的版本不匹配引起的。一旦我将另一个项目升级到版本 9.0.0,它就开始工作了。

【讨论】:

  • 在我的情况下,我不得不将 2 个 AutoMapper 包的版本设置为 2 个不同的版本,这与其包的版本不太一致。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-30
  • 2016-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多