2014-06-16 2 views
0

나는 서로 매핑해야하는 두 개의 개체가 있습니다. 모양이자동 매핑 장치를 사용하여 사용자 지정 리졸버를 사용하여 목록 매핑

public class Example1 
{ 
    CustomType1 Prop { get; set; } 
    List<CustomType1> List { get; set; } 
} 

public class Example2 
{ 
    Customtype2 Prop { get; set; } 
    List<Customtype2> List { get; set; } 
} 

public class CustomType1 
{ 
    public string SomeString { get; set; } 
} 

public class Customtype2 
{ 
    public string FirstPartOfSomeString { get; set; } 

    public string SecondPartOfSomeString { get; set; } 
} 

CustomType1을 CustomType2에 매핑 한 CustomResolver를 하나 만들고이 리졸버를 목록에 사용하려고합니다. 예를 들어,

Mapper.CreateMap<Example1, Example2>() 
      .ForMember(d => d.Prop, opt => opt.ResolveUsing(myCustomResolver)) 
      .ForMember(d => d.List, opt => opt.ResolveUsing(/*use myCustomResolver on a list here*/)); 

나는 같은 것을 사용하여 시도했다 :

Mapper.CreateMap<Example1, Example2>() 
      .ForMember(d => d.Prop, opt => opt.ResolveUsing(myCustomResolver)) 
      .ForMember(d => d.List, opt => opt.MapFrom(s => s.List.Select(myCustomResolver.Resolve).ToList())); 

을하지만 난 뭔가를 놓친 것 같다. AutoMapper로이 작업을 수행 할 수있는 방법이 있습니까?

+0

CustomResolver 코드를 게시하십시오. –

답변

1

해결 프로그램을 사용하는 대신 사용자 지정 형식간에 매핑을 추가하려고 시도 했습니까? AutoMapper는 목록에 대한 매핑을 재사용하기에 충분히 지능적입니다 ...

Mapper.CreateMap<CustomType1, CustomType2>() 
    .ForMember(x => FirstPartOfSomeString, opts => opts.MapFrom(x => x.SomeString.Substring(5))) 
    .ForMember(x => SecondPartOfSomeString, opts => opts.MapFrom(x => x.SomeString.Substring(5, 5))); 

Mapper.CreateMap<Example1, Example2>(); 
+0

이것은 효과가 있습니다. 콜렉션을 맵핑하기 전에 CustomType1에서 CustomType2 맵으로 정의한 경우, 맵핑을 목록과 재사용 할 수 있습니다. 감사. – Mike