0
이의 내가 다음과 같은 클래스 AutoMapper : Unflattening는
class Foo
{
public Bar First { get; }
public Bar Second { get; }
public Foo(Bar first, Bar second)
{
First = first;
Second = second;
}
}
class Bar
{
public string Name { get; set; }
}
class FooDto
{
public string FirstName { get; set; }
public string SecondName { get; set; }
}
이 내가
Foo
에
FooDto
를 매핑 할 가정 해 봅시다 생성자. 나는 약
Reverse Mapping and Unflattening 그래서 설정 내 confoguration과 같이 읽은 :
IMapper mapper = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Foo, FooDto>()
.ReverseMap();
}).CreateMapper();
불행하게도이
System.ArgumentException 실패 : Test.AutoMapper.Foo 0 인수 또는 단지와 생성자를 가질 필요가 선택적 args.
모든 생성자 매개 변수를 수동으로 매핑하지 않고 Foo
생성자를 사용하도록 AutoMapper를 구성하려면 어떻게해야합니까? (내 실제 코드에서는 형식에 더 많은 속성이 있습니다.)
방법은'FooDto'에서 매핑 할 수 있습니다에'(ReverseMap에 의해 암시) Foo'? 생성자에게 전달하기 위해'Bar' 인스턴스를 2 개 가져야하지만, 어디에서? – Evk
@Evk'Foo' 생성자를 제거하고 속성을 변경할 수 있도록하여 작동하는지 확인할 수 있습니다. AutoMapper는 자동으로'FirstName'과'SecondName'을 비평 화하여 2 개의'Bar' 인스턴스를 만듭니다. 단지 생성자를 호출 할 수 없습니다. –