当出于某种原因使用Automappers IMemberValueResolver时,TDestMember始终为0或null,是否有任何想法?
public class CustomResolver : IMemberValueResolver<object, object, int, int>
{
public int Resolve(object source, object destination, int sourceMember, int destinationMember, ResolutionContext context)
{
return 1;
}
}
我如何称呼它的一个例子:
.ForMember(dest => dest.SortOrder,
opt => opt.ResolveUsing<CustomResolver, int>(src => src.Id));
答案 0 :(得分:0)
当您未将目标对象传递给Map时会发生这种情况。
答案 1 :(得分:0)
假设您的地图看起来像这样:
CreateMap<Source, Destination>
.ForMember(dest => dest.SortOrder)
opt => opt.ResolveUsing<CustomResolver, int>(src => src.Id));
您的CustomResolver如下所示:
public class CustomResolver : IMemberValueResolver<Source, Destination, int, int>
{
public int Resolve(Source source, Destination destination, int sourceMember, int destinationMember, ResolutionContext context)
{
return 1;
}
}
TDestMember (destinationMember
参数)将为null或解析器内的默认值,因为尚未映射。 sourceMember
参数将是地图中的谓词(src => src.Id
)的值。
通常,仅在需要在解析逻辑所需的谓词中传递特定值时才使用IMemberValueResolver<>
。否则,如果只需要源对象和/或目标对象,则应使用IValueResolver<>
。