1、 record定义及使用
参考文档:.NET C# 9.0 record和with的定义及使用
2、扩展方法
实现代码如下,
public record BFrom
{
public Guid Id { get; init; }
public Guid DbExtraId { get; init; }
}
public static class AutoMapperExtensions
{
public static IMappingExpression<TSource, TDestination> MapRecordMember<TSource, TDestination, TMember>(
this IMappingExpression<TSource, TDestination> mappingExpression,
Expression<Func<TDestination, TMember>> destinationMember, Expression<Func<TSource, TMember>> sourceMember)
{
var memberInfo = ReflectionHelper.FindProperty(destinationMember);
string memberName = memberInfo.Name;
return mappingExpression
.ForMember(destinationMember, opt => opt.MapFrom(sourceMember))
.ForCtorParam(memberName, opt => opt.MapFrom(sourceMember));
}
}
调用方法:
CreateMap<BFrom, ATo>()
.MapRecordMember(a => a.ExtraId, src => src.DbExtraId)
.ReverseMap();