Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper - how to use type converter on a single property

Tags:

c#

automapper

Is it possible to use AutoMapper with a single property? I would like to convert a string value of comma delimited values to a list of values separated by a line feed on the UI.

The current custom converters only seem to work at the class level, and because the dest and source types are both string I can't create a single map based on .

How would I apply the custom converter on a single property? Or should the custom resolver be used instead?

like image 822
jaffa Avatar asked Jun 14 '11 10:06

jaffa


People also ask

How do I ignore source property AutoMapper?

So, the AutoMapper Ignore() method is used when you want to completely ignore the property in the mapping. The ignored property could be in either the source or the destination object.

Can AutoMapper map collections?

AutoMapper supports polymorphic arrays and collections, such that derived source/destination types are used if found.


1 Answers

You may use a custom resolver or map the property by calling your convert logic in a MapFrom lambda:

Mapper.CreateMap<TSource, TDest>().ForMember(dto => dto.DestPrp,
                                                        e => e.MapFrom(o => ConvertTo(o.SourceProp)))
like image 133
ondrejsv Avatar answered Sep 27 '22 18:09

ondrejsv