gpt4 book ai didi

c# - Automapper Aftermap "method not found"异常

转载 作者:行者123 更新时间:2023-12-05 03:44:39 24 4
gpt4 key购买 nike

我正在尝试使用 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>)'.

我是否缺少其他一些配置?如何让我的代码工作?

最佳答案

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

关于c# - Automapper Aftermap "method not found"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66364273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com