我有两种类型.一个是实体,一个是模型.实体有一个名为Items
的属性,它是一个字符串.模型有一个属性,也称为Items
,但它是字符串的List<string>
.
我正在使用AutoMapper来映射这个—像这样:
CreateMap<Model, Entity>()
.ForMember(dest => dest.Items,
opt => opt.ConvertUsing<List<string>>(new ListToStringValueConverter()));
以下是我的ListToStringValueConverter
:
public class ListToStringValueConverter : IValueConverter<List<string>, string>
{
public string Convert(List<string> source, ResolutionContext context)
{
if (source != null)
return string.Join(",", source);
return string.Empty;
}
}
然而,当对模型中的实体进行映射时,
entity = _mapper.Map<Entity>(model);
我得到entity.Items
的值,
System.Collections.Generic.List`1[System.String]
Why is my converter not converting the ["1","2"] to "1,2" but to
System.Collections.Generic.List
1[System.String]`?
我错过了什么或做错了什么?