Asp.net mvc Automapper AddAfterMapAction未调用方法

Asp.net mvc Automapper AddAfterMapAction未调用方法,asp.net-mvc,automapper,automapper-9,Asp.net Mvc,Automapper,Automapper 9,我正在使用全局配置进行Automapper配置文件映射 public class StudentProfile : Profile { public StudentProfile() { CreateMap<Student, StudentVM>() .ForMember(dest => dest.school, src => src.Ignore()); } } 现在我使用表达式添加.AddAfterMa

我正在使用全局配置进行Automapper配置文件映射

public class StudentProfile : Profile
{
    public StudentProfile()
    {
        CreateMap<Student, StudentVM>()
            .ForMember(dest => dest.school, src => src.Ignore());
    }
}
现在我使用表达式添加.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);
    }
static void Main(字符串[]args)
{
尝试
{
var mapper=Configuration.InitializeAutoMapper();
foreach(mapper.ConfigurationProvider.GetAllTypeMaps()中的变量项)
{
表达式beforeMapAction=(x)=>Test(x);
项目.添加后映射操作(映射操作前);
}
var dest=mapper.Map(StudentService.GetStudent());
Console.ReadLine();
}
捕获(例外情况除外)
{
}
}
公共静态空隙试验(int x)
{
WriteLine(“X={0}”,X);
}
当我使用此行进行映射时,它没有调用测试方法:
var dest=mapper.Map(StudentService.GetStudent())


我做错什么了吗。因为它应该在映射时调用测试方法。

在实例化
MappingConfiguration
后,您不能修改映射。一旦构建了
TypeMap
,执行计划就会被创建,并且不能更改

public class StudentProfile : Profile
{
    public StudentProfile()
    {
        CreateMap<Student, StudentVM>()
            .ForMember(dest => dest.school, src => src.Ignore());
    }
}

您需要将
AfterMap
配置移动到您正在配置的位置。

实例化
MappingConfiguration
后,您无法修改映射。一旦构建了
TypeMap
,执行计划就会被创建,并且不能更改


您需要将
AfterMap
配置移动到您正在配置的位置。

@ZoltánTamási感谢您的回复。它只允许在.AddAfterMapAction中使用表达式。@ZoltánTamási感谢您的回复。它只允许在.AddAfterMapAction中使用表达式。