AutoMapper/AutoMapper — Issue #137: InvalidCastException when mapping child type to target type with the same name
Author: asbjornu
Created: 2011-10-24 15:20:32+00:00
State: open

I've written up a [test](https://github.com/asbjornu/AutoMapper/blob/master/src/UnitTests/Asbjornu/AsbjornuTest.cs) that purports a bug introduced in later versions of AutoMapper. I haven't investigated exactly when the problem was introduced, but it worked in version 1.1.0.188.

The problem seems to be very specific, which is why I haven't been able to create a more general test case. The test does show the problem clearly, though, which is that mapping `AutoMapper.UnitTests.Asbjornu.Domain.ContactInfo` to `AutoMapper.UnitTests.Asbjornu.Models.ContactInfo` directly works just fine, while mapping from the former to the latter when the former is contained in a `Customer` class, doesn't.

When mapping `ContactInfo` as a child object, AutoMapper throws the following exception:

AsbjornuTest.Map_Customer_ReturnsCustomerModel : FailedAutoMapper.AutoMapperMappingException : Trying to map AutoMapper.UnitTests.Asbjornu.Domain.Customer to AutoMapper.UnitTests.Asbjornu.Models.CustomerModel.
Using mapping configuration for AutoMapper.UnitTests.Asbjornu.Domain.Customer to AutoMapper.UnitTests.Asbjornu.Models.CustomerModel
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
----> System.InvalidCastException : Unable to cast object of type 'AutoMapper.UnitTests.Asbjornu.Domain.ContactInfo' to type 'AutoMapper.UnitTests.Asbjornu.Models.ContactInfo'.
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) in MappingEngine.cs: line 314
at AutoMapper.MappingEngine.Map(Object source, Type sourceType, Type destinationType, Action`1 opts) in MappingEngine.cs: line 98
at AutoMapper.MappingEngine.Map(Object source, Action`1 opts) in MappingEngine.cs: line 49
at AutoMapper.MappingEngine.Map(Object source) in MappingEngine.cs: line 38
at AutoMapper.Mapper.Map(Object source) in Mapper.cs: line 27
at AutoMapper.UnitTests.Asbjornu.AsbjornuTest.Map_Customer_ReturnsCustomerModel() in AsbjornuTest.cs: line 70
--InvalidCastException
at lambda_method(Closure, Object[])
at AutoMapper.ConstructorMap.ResolveValue(ResolutionContext context) in ConstructorMap.cs: line 29
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.CreateObject(ResolutionContext context) in MappingEngine.cs: line 354
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.NewObjectPropertyMapMappingStrategy.GetMappedObject(ResolutionContext context, IMappingEngineRunner mapper) in TypeMapObjectMapperRegistry.cs: line 164
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.Map(ResolutionContext context, IMappingEngineRunner mapper) in TypeMapObjectMapperRegistry.cs: line 62
at AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper) in TypeMapMapper.cs: line 22
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) in MappingEngine.cs: line 308