是否可以使用AutoMapper做到这一点:将源对象放在目标对象的私有属性中。
以下是我想做的事:
using System;
using System.Diagnostics;
using AutoMapper;
namespace ConsoleApp12 {
class B {
public B() { }
}
class A {
public B B { get; private set; }
public A() { }
}
class Program {
static void Main(string[] args) {
var config = new MapperConfiguration(cfg =>
cfg.CreateMap<B,A>()
.ForMember(ma => ma.B,mb => mb) //Error mb can't be "this"
);
var mapper = new Mapper(config);
var b = new B();
var a = mapper.Map<A>(b);
Debug.Assert(a.B != null);
}
}
}