Automapper映射字符串到列表 错误地

问题描述 投票:0回答:1

我有两堂课

 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中的ProviderProviders,并且映射正常工作。但是,在使两个类的拼写一致之后,映射将无法正确进行。

"Test"从Source类映射到["T", "e", "s", "t"]。当每个类的属性名称不同时,映射将正常工作。

c# automapper
1个回答
1
投票

我用过:

  • 控制台应用程序
  • 。NET 4.6.1
  • 最新的稳定版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
© www.soinside.com 2019 - 2024. All rights reserved.