1
如何使Automapper在不创建新对象的情况下使用精确值?如何使Automapper在不创建新对象的情况下使用精确值
using System.Collections.Generic;
using AutoMapper;
namespace Program
{
public class A { }
public class B
{
public A Aprop { get; set; }
}
public class C
{
public A Aprop { get; set; }
}
class Program
{
private static void Main(string[] args)
{
AutoMapper.Mapper.Initialize(cnf =>
{
// I really need this mapping. Some additional Ignores are present here.
cnf.CreateMap<A, A>();
// The next mapping should be configured somehow
cnf.CreateMap<B, C>(); //.ForMember(d => d.Aprop, opt => opt.MapFrom(...)) ???
});
A a = new A();
B b = new B() {Aprop = a};
C c = Mapper.Map<C>(b);
var refToSameObject = b.Aprop.Equals(c.Aprop); // Evaluates to false
}
}
}
我应该如何改变,以使cnf.CreateMap<B, C>();
线refToSameObject
变量有true
价值?如果我删除cnf.CreateMap<A, A>();
它将以这种方式工作,但我无法删除它,因为有时我会使用automapper从其他A
类更新A
类。解决此