Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get destination type from Automapper.Mapper

Tags:

c#

automapper

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) ?

like image 980
Silviu Preda Avatar asked Aug 08 '13 13:08

Silviu Preda


1 Answers

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);
}
like image 98
nemesv Avatar answered Nov 20 '22 04:11

nemesv