Misu Costel Misu Costel - 27 days ago 6
C# Question

Get destination type from Automapper.Mapper

I have used Automapper for some time now, and it works very neat.
I have the following mapping:

Mapper.CreateMap<Models.MyModel,Entities.MyEntity>();


Is there any way, any method that, provided
typeof(Models.MyModel)
will return
typeof(Entities.MyEntity)
?

Answer Source

You can get all the registered TypeMaps (Automapper's type for storing source-destination type pairs and other mapping related information) with the Mapper.GetAllTypeMaps() method.

Using the typemaps you can search for you source type:

[Test]
public void Test()
{
    Mapper.CreateMap<Models.MyModel, Entities.MyEntity>();
    var destination = Mapper.GetAllTypeMaps()
                            .First(t => t.SourceType == typeof(Models.MyModel));
    Assert.AreEqual(typeof (Entities.MyEntity), destination.DestinationType);
}