AutoMapper/AutoMapper — Issue #153: Mapping 2 abstract derived types fails
Author: JensSkidt
Created: 2011-11-28 18:13:51+00:00
State: open

Not sure if this is the wrong way to map, but when having two different classes, which derive from the same abstract class, then the first mapping seems to go ok, while the other seems to throw an exception.

Below is a test-sample:

[Test]
public void Example()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap();

cfg.CreateMap()
.Include()
.Include();

cfg.CreateMap()
.Include();

cfg.CreateMap();
});

var expectedCSrc = new CSrc() { StringA = "A", StringB = "B", StringC = "C" };
var expectedBSrc = new BSrc() { StringA = "A", StringB = "B" };

var expectedContCSrc = new ContainsASrc() { A = expectedCSrc };
var expectedContBSrc = new ContainsASrc() { A = expectedBSrc };

var actualContCDest = Mapper.Map(expectedContCSrc);
var actualContBDest = Mapper.Map(expectedContBSrc); // THROWS

Mapper.AssertConfigurationIsValid();
Assert.IsNotNull(actualContBDest);
Assert.IsNotNull(actualContCDest);
}

public class ContainsASrc
{
public ASrc A { get; set; }
}

public abstract class ASrc
{
public string StringA { get; set; }
}

public class BSrc : ASrc
{
public string StringB { get; set; }
}

public class CSrc : BSrc
{
public string StringC { get; set; }
}

public class ContainsADest
{
public ADest A { get; set; }
}

public abstract class ADest
{
public string StringA { get; set; }
}

public class BDest : ADest
{
public string StringB { get; set; }
}

public class CDest : BDest
{
public string StringC { get; set; }
}
}

Exception details:
AutoMapper.AutoMapperMappingException : Trying to map Domstolene.JFS.Sagsstyring.GUI.Tests.Unittests.TestAbstractExample+ContainsASrc to Domstolene.JFS.Sagsstyring.GUI.Tests.Unittests.TestAbstractExample+ContainsADest.
Using mapping configuration for Domstolene.JFS.Sagsstyring.GUI.Tests.Unittests.TestAbstractExample+ContainsASrc to Domstolene.JFS.Sagsstyring.GUI.Tests.Unittests.TestAbstractExample+ContainsADest
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
----> AutoMapper.AutoMapperMappingException : Trying to map Domstolene.JFS.Sagsstyring.GUI.Tests.Unittests.TestAbstractExample+CSrc to Domstolene.JFS.Sagsstyring.GUI.Tests.Unittests.TestAbstractExample+CDest.
Using mapping configuration for Domstolene.JFS.Sagsstyring.GUI.Tests.Unittests.TestAbstractExample+CSrc to Domstolene.JFS.Sagsstyring.GUI.Tests.Unittests.TestAbstractExample+CDest
Destination property: A
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
----> AutoMapper.AutoMapperMappingException : Trying to map Domstolene.JFS.Sagsstyring.GUI.Tests.Unittests.TestAbstractExample+CSrc to Domstolene.JFS.Sagsstyring.GUI.Tests.Unittests.TestAbstractExample+CDest.
Using mapping configuration for Domstolene.JFS.Sagsstyring.GUI.Tests.Unittests.TestAbstractExample+CSrc to Domstolene.JFS.Sagsstyring.GUI.Tests.Unittests.TestAbstractExample+CDest
Destination property: A
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
----> AutoMapper.AutoMapperMappingException : Trying to map Domstolene.JFS.Sagsstyring.GUI.Tests.Unittests.TestAbstractExample+BSrc to System.String.
Using mapping configuration for Domstolene.JFS.Sagsstyring.GUI.Tests.Unittests.TestAbstractExample+CSrc to Domstolene.JFS.Sagsstyring.GUI.Tests.Unittests.TestAbstractExample+CDest
Destination property: StringC
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
----> System.InvalidCastException : Unable to cast object of type 'BSrc' to type 'CSrc'.