属性名称不同时 Automapper 的使用 [英] Usage of Automapper when property names are different

查看:54
本文介绍了属性名称不同时 Automapper 的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 Codeplex 的 AutoMapper,对我来说,目标对象具有以Field"结尾的所有属性,即 cityField,而源对象只有 city.

We are using AutoMapper from Codeplex and for me the destination object has all the properties ending with 'Field', ie cityField and the source object has just city.

我可以使用下面的代码来实现,但所有属性都只是以Field"为后缀,并且有 20 个属性.

I can use the below code to achieve but all of the properties are just suffixed with 'Field' and there are 20 properties.

.ForMember(dest => dest.cityField, opt => opt.MapFrom(origin => origin.City));

有没有其他方法可以在映射时忽略 'Field' 词,以便它可以在不使用 .ForMember() 20 次的情况下进行映射?

Is there any other way to ignore 'Field' word when mapping and so that it can map without using .ForMember() 20 times?

推荐答案

您可以尝试识别后缀:

Mapper.Initialize(cfg => {
    cfg.RecognizePostfixes("Field");
    cfg.CreateMap<Source, Dest>();
});

识别前缀也适用于配置文件,如果它只是适用于一组地图.

Recognizing prefixes also works local to profiles, if it's just a set of maps that this applies to.

这篇关于属性名称不同时 Automapper 的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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