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>();
을 제거하면이 방법으로 작동하지만 가끔은 오토캡을 사용하여 A
클래스를 다른 A
클래스에서 업데이트하기 때문에 제거 할 수 없습니다.
AutoMapper.Mapper.Initialize(cnf =>
{
cnf.CreateMap<A, A>();
cnf.CreateMap<B, C>()
.ConstructUsing(src => new C() { Aprop = src.Aprop })
.ForMember(dest => dest.Aprop, opt => opt.Ignore());
});
이 일을해야하고 정말 그냥 하나 개의 속성의 가정이 너무 고통스러운되지 않습니다 :이 약