0

我需要:

// SOURCE
class A { 
  public B B;
  public C C;
}

class B { public int X; }
class C { public int Y; }

// DESTINATION
class D { 
  public int X; 
  public int Y;
}

// I want to configure following operation
// without mapping each (X and Y) fields explicitly.
D.X = A.B.X;
D.Y = A.C.Y;


// here is the essence of what I am trying to accomplish.
Mapper.CreateMap<B, D>();
Mapper.CreateMap<C, D>();
var map = Mapper.CreateMap<A, D>();
map.ForMember(dst => dst, opt => opt.MapFrom(src => src.B));
map.ForMember(dst => dst, opt => opt.MapFrom(src => src.C));

有什么建议么?

注意:我知道如果我将 DX 更改为 D.BX 并将 DY 更改为 D.CY 它将起作用。我想在不添加 D 属性前缀的情况下完成此操作。我也可以通过使用地图的“填充”版本来实现这一点。

4

1 回答 1

0
// I want to configure following operation
// without mapping each (X and Y) fields explicitly.
D.X = A.B.X;
D.Y = A.B.Y;

根据你的班级,B 没有 Y。B 有 X。你的意思是D.Y = A.C.Y?如果是这样:

Mapper.CreateMap<A, D>()
    .ForMember(d => d.X, o => o.MapFrom(s => s.B.X))
    .ForMember(d => d.Y, o => o.MapFrom(s => s.C.Y))
;
于 2012-07-26T22:59:57.900 回答