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 null
, Automapper sets the destination property to a zero-length array instead of null
as I expected.
Is there a way to configure Automapper to set the destination to null
when the source is null
?
In case my explanation is unclear, the following code illustrates what I'm trying to describe:
public class Test
{
public byte[] ByteArray { get; set; }
public int? NullableInt { get; set; }
public int Int { get; set; }
}
class Program
{
static void Main(string[] args)
{
Mapper.CreateMap<Test, Test>();
var test1 = new Test { Int = 123, NullableInt = null, ByteArray = null };
var test2 = Mapper.Map<Test>(test1);
// test1: Int == 123, NullableInt == null, ByteArray == null
// test2: Int == 123, NullableInt == null, ByteArray == byte[0] <-- expect this to be null
}
}