Automapper sets an array property to a zero-length array rather than null

后端 未结 2 1482
眼角桃花
眼角桃花 2021-01-17 07:37

I\'m using Automapper to copy values from one instance to another, and I\'m finding that if the class has an array property, and the source instance has the property set to

相关标签:
2条回答
  • 2021-01-17 08:08

    I found that this was already reported as an issue, and a new configuration option was added (see this commit). At this time, the option is not in the release available via NuGet, but I was able to figure out a way to handle this until the next version is released:

    Mapper.CreateMap<Test, Test>()
        .ForMember(t => t.ByteArray, opt => opt.ResolveUsing(t => t.ByteArray == null ? null : t.ByteArray));
    

    Update:

    As of version 2.1.265.0, you can using the AllowNullCollections property:

    Mapper.Configuration.AllowNullCollections = true;
    Mapper.CreateMap<Test, Test>();
    
    0 讨论(0)
  • 2021-01-17 08:11

    I think this is just a quirk from using the exact same type for both source and destination. If you actually make them different types, the byte array comes through as null.

    0 讨论(0)
提交回复
热议问题