Using AutoMapper to map the property of an object to a string

本小妞迷上赌 提交于 2019-11-30 07:54:30

This is because you are trying to map to the actual destination type rather than a property of the destination type. You can achieve what you want with:

Mapper.CreateMap<Tag, string>().ConvertUsing(source => source.Name ?? string.Empty);

although it would be a lot simpler just to override ToString on the Tag class.

ZafarYousafi

ForMember means you are providing mapping for a member where you want a mapping between type. Instead, use this :

Mapper.CreateMap<Tag, String>().ConvertUsing<TagToStringConverter>();

And Converter is

public class TagToStringConverter : ITypeConverter<Tag, String>
{
    public string Convert(ResolutionContext context)
    {
        return (context.SourceValue as Tag).Name ?? string.Empty;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!