Я использую глобальную конфигурацию для сопоставления профилей Automapper.
public class StudentProfile : Profile
{
public StudentProfile()
{
CreateMap<Student, StudentVM>()
.ForMember(dest => dest.school, src => src.Ignore());
}
}
Конфигурация Mapper
public static class Configuration
{
public static IMapper InitializeAutoMapper()
{
MapperConfiguration config = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new StudentProfile());
});
config.AssertConfigurationIsValid();
return config.CreateMapper();
}
}
Теперь я добавляю .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 при отображении.