我如何配置automapper来映射它:
class Source { Guid Id; double Price; }
对此:
class Destination { Guid Id; DestinationDifference Difference; }
class DestinationDifference { decimal Amount; }
答案 0 :(得分:1)
首先:您应该阅读有关如何发布问题以及应添加哪些信息的常见问题解答。 (不,我不是下来的人)
以下是如何使映射正常工作的示例。请注意,我已经更改了一些类,因为AutoMapper需要属性。
Source source = new Source();
source.Id = Guid.NewGuid();
source.Price = 10.0;
Mapper.Initialize(x => x.CreateMap<Source, Destination>()
.ForMember(a => a.Difference,
b => b.MapFrom(s => new DestinationDifference() { Amount = (decimal)s.Price })));
Destination destination = Mapper.Map<Source, Destination>(source);
类:
class Source
{
public Guid Id { get; set; }
public double Price { get; set; }
}
class Destination
{
public Guid Id { get; set; }
public DestinationDifference Difference { get; set; }
}
class DestinationDifference
{
public decimal Amount { get; set; }
}