在AfterMap中不使用Automapper的全局Mapper变量 [英] Use no global Mapper variable of Automapper in AfterMap

查看:188
本文介绍了在AfterMap中不使用Automapper的全局Mapper变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用AfterMap时是否可以在AutoMapper中获取当前的映射上下文?

Is there a way to get the current mapping context in AutoMapper when using AfterMap?

public class DefaultMappingProfile : Profile
{
    protected override void Configure()
    {
        this.CreateMap<SomeList, List<SpecialItem>>()
            .AfterMap((src, dst) => dst.AddRange(
                 src.elem.Select(Mapper.Map<SpecialItem>)));

我尝试使用.ConstructUsing(context => {}),但这给了我与使用AfterMap(!?)时不同的结果.但是我不想在这里访问全局变量Mapper.有没有办法在这里访问全局变量?

I tried to use .ConstructUsing(context => {}) but this gave me not the same results as when using AfterMap (!?). But I don't want to access the global variable Mapper here. Is there a way to get around accessing the global variable here?

推荐答案

您也可以使用ConvertUsing<TSource, TDestination>,例如:

CreateMap<data.BillCycle, domain.BillCycle>().ConvertUsing<BillCycleConverter>();

该类将实现ITypeConverter<TSource, TDestination>,并允许您访问映射器:

The class will implement ITypeConverter<TSource, TDestination> and gives you access to the mapper:

public class BillCycleConverter : ITypeConverter<data.BillCycle, domain.BillCycle>
{
    public domain.BillCycle Convert(ResolutionContext context)
    {
        context.Engine.Map<X, Y>...
    }
}

自定义类型转换器

这篇关于在AfterMap中不使用Automapper的全局Mapper变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆