Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

c# - Usage of Automapper when property names are different

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.

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));

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try recognizing postfixes:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...