passing around values to an AutoMapper Type Converter from outside

风流意气都作罢 提交于 2019-12-03 00:36:19

Just use the Map overload that takes a Action<IMappingOperationOptions>. You can add configuration elements to the Items property that are then passed to your ITypeConverter

public class CustomConverter : ITypeConverter<string, string>
{
    public string Convert(ResolutionContext context)
    {
        return "translated in " + context.Options.Items["language"];
    }
}

internal class Program
{
    private static void Main(string[] args)
    {
        AutoMapper.Mapper.CreateMap<string, string>().ConvertUsing<CustomConverter>();
        var result = AutoMapper.Mapper.Map<string, string>("value" , opt => opt.Items["language"] = "english");
        Console.Write(result); // prints "translated in english"
        Console.ReadLine();
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!