【发布时间】:2020-02-24 10:32:57
【问题描述】:
我正在为 Automapper 配置文件映射使用全局配置。
public class StudentProfile : Profile
{
public StudentProfile()
{
CreateMap<Student, StudentVM>()
.ForMember(dest => dest.school, src => src.Ignore());
}
}
映射器配置
public static class Configuration
{
public static IMapper InitializeAutoMapper()
{
MapperConfiguration config = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new StudentProfile());
});
config.AssertConfigurationIsValid();
return config.CreateMapper();
}
}
现在我正在使用 Expression 添加 .AddAfterMapAction。
static void Main(string[] args)
{
try
{
var mapper = Configuration.InitializeAutoMapper();
foreach (var item in mapper.ConfigurationProvider.GetAllTypeMaps())
{
Expression<Action<int>> beforeMapAction = (x) => Test(x);
item.AddAfterMapAction(beforeMapAction);
}
var dest = mapper.Map<Student, StudentVM>(StudentService.GetStudent());
Console.ReadLine();
}
catch (Exception ex)
{
}
}
public static void Test(int x)
{
Console.WriteLine("X = {0}", x);
}
当我使用这一行进行映射时,它没有调用测试方法:var dest = mapper.Map<Student, StudentVM>(StudentService.GetStudent());
我在这里做错什么了吗?因为它应该在映射时调用 Test 方法。
【问题讨论】:
-
@ZoltánTamási 感谢您的回复。它只允许 Expression in.AddAfterMapAction.
标签: asp.net-mvc automapper automapper-9