【发布时间】: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