I have this code example done in LinqPad 5 (.Net Framework):
void Main()
{
var o = new A() { b = new B() { Msj = "Hello, world!" } };
var r = MapObject<C, A>(o);
r.Dump();
}
public class A
{
public B b {get;set;}
}
public class B
{
public string Msj {get;set;}
}
public class C
{
public D b { get; set; }
}
public class D
{
public string Msj { get; set; }
}
private static Tdst MapObject<Tdst, Tori>(Tori obj)
{
var configMapper = new AutoMapper.MapperConfiguration(cfg => cfg.CreateMap<Tori, Tdst>());
var mapper = configMapper.CreateMapper();
return mapper.Map<Tdst>(obj);
}
This code work well in AutoMapper version 7, but I have upgraded AutoMapper from version 7 to version 10. Now this code throws an error:
AutoMapperMappingException: Error mapping types.
Mapping types:
A -> C
UserQuery+A -> UserQuery+C
Type Map configuration:
A -> C
UserQuery+A -> UserQuery+C
Destination Member:
b
I know that classes A and C are exactly the same, but they were created from different web services so they are in different namespaces.
How could I change the method MapObject to make it work in the same way that version 7 of AutoMapper?
Edit 1:
I think I solved it using Mapster instead of AutoMapper, but I want let the question open just for curiosity.
If you know what classes are "identical", it's easy to write some code that creates the needed maps. AM no longer does that for you because it cannot really know when such a default map is good enough.