http://docs.automapper.org/en/stable/Value-converters.html
public class CurrencyFormatter : IValueConverter<decimal, string> {
public string Convert(decimal source)
=> source.ToString("c");
}
cfg.CreateMap<Order, OrderDto>()
.ForMember(d => d.Amount, opt => opt.ConvertUsing(new CurrencyFormatter()));
转换器 与 解释器 区别
- Type converter =
Func<TSource, TDestination, TDestination> // 全局
- Value resolver =
Func<TSource, TDestination, TDestinationMember> //
- Member value resolver =
Func<TSource, TDestination, TSourceMember, TDestinationMember>
- Value converter =
Func<TSourceMember, TDestinationMember> //只与成员相关
-
AutoMapper 值转换器