简体   繁体   English

Automapper 将字符串映射到列表<string>错误地

[英]Automapper mapping string to list<string> incorrectly

I have two classes我有两节课

 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; }
}

My mapping is as follows:我的映射如下:

CreateMap<SourceClass, DestinationClass>()
    .ForMember(destinationMember => destinationMember.Provider,
        memberOptions => memberOptions.MapFrom(src => 
            new List<string> { src.Provider ?? "" }));

Now, previously Provider in the DestinationClass was Providers and the mapping worked as normal.现在,以前Provider的DestinationClass是Providers和测绘工作正常。 However, after making the spelling in both classes consistent, the mapping fails to happen properly.但是,在使两个类中的拼写一致后,映射无法正常进行。

"Test" from Source class maps to ["T", "e", "s", "t"] . Source 类中的"Test"映射到["T", "e", "s", "t"] When the property names were different in each class, the mapping worked correctly.当每个类中的属性名称不同时,映射工作正常。

I've used:我用过:

  • Console Application控制台应用程序
  • .NET 4.6.1 .NET 4.6.1
  • the latest stable Automapper最新的稳定版Automapper

Such behaviour is not reproducible:这种行为是不可重现的:

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 ?? "" })));
}

Output:输出:

FooProvider

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM