这是一个用于说明问题的小代码段。
[TestMethod]
public void AutoMapper_MappingNullValue_ShouldMapNull()
{
//Arrange
var source = new DummySource { MyDate = null };
var destination = new DummyDestination { MyDate = DateTime.Now };
AutoMapper.Mapper.CreateMap<DummySource, DummyDestination>();
//Act
var returnResult = AutoMapper.Mapper.Map(source, destination);
//Assert
Assert.IsNull(returnResult.MyDate);
Assert.IsNotNull(destination.MyDate);
}
private class DummySource
{
public DateTime? MyDate { get; set; }
}
private class DummyDestination
{
public DateTime? MyDate { get; set; }
}
为什么第一个断言失败?我希望结果上有一个空值。实际上,returnResult
和destination
都具有初始值。