我有两堂课
public class SourceClass
{
public Guid Id { get; set; }
public string Provider { get; set; }
}
public class DestinationClass
{
public Guid Id { get; set; }
public List<string> Provider { get; set; }
}
我的映射如下:
CreateMap<SourceClass, DestinationClass>()
.ForMember(destinationMember => destinationMember.Provider,
memberOptions => memberOptions.MapFrom(src =>
new List<string> { src.Provider ?? "" }));
[现在,以前DestinationClass中的Provider
为Providers
,并且映射正常工作。但是,在使两个类的拼写一致之后,映射将无法正确进行。
"Test"
从Source类映射到["T", "e", "s", "t"]
。当每个类的属性名称不同时,映射将正常工作。
我用过:
Automapper
这种行为不可复制:
static MapperConfiguration _conf;
static void Main(string[] args)
{
var src = new SourceClass() { Id = Guid.NewGuid(), Provider = "FooProvider" };
InitializeAutomapper();
var mapper = _conf.CreateMapper();
DestinationClass destinationClass = mapper.Map<DestinationClass>(src);
Console.WriteLine(destinationClass.Provider[0]);
}
static void InitializeAutomapper()
{
_conf = new MapperConfiguration(cfg =>
cfg.CreateMap<SourceClass, DestinationClass>()
.ForMember(destinationMember => destinationMember.Provider,
memberOptions => memberOptions.MapFrom(src =>
new List<string> { src.Provider ?? "" })));
}
输出:
FooProvider