使用AutoMapper< = 3.0,以下测试通过。
public class AutoMapperTest
{
static Source source;
static Destination destination;
Establish context = () =>
{
Mapper.Configuration.AllowNullCollections = false;
Mapper.CreateMap<Source, Destination>();
source = new Source { Name = null, Data = null };
};
Because of = () => destination = Mapper.Map<Destination>(source);
It should_map_name_to_null = () => destination.Name.ShouldBeNull();
It should_map_array_to_empty = () => destination.Data.ShouldNotBeNull();
}
public class Source
{
public string Name { get; set; }
public string[] Data { get; set; }
}
public class Destination
{
public string Name { get; set; }
public string[] Data { get; set; }
}
从版本3.1开始,should_map_array_to_empty断言失败,因为destination.Data设置为null而不是之前的空数组。有没有办法恢复以前的行为,最好是全局,而不是单独按配置的地图?
配置选项Mapper.Configuration.AllowNullCollections = false
在这种情况下似乎没有任何区别,无论我尝试过哪种版本的AutoMapper。