【发布时间】:2014-08-05 21:16:13
【问题描述】:
我有 2 个对象需要相互映射。它们看起来像
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 做到这一点?
【问题讨论】:
-
请发布 CustomResolver 代码。
标签: c# collections mapping automapper