Search code examples
c#dictionaryexpressionautomapper

Get all the defined mappings from an AutoMapper defined mapping


Let's assume that I've two classes : CD and CDModel, and the mapping is defined as follows:

Mapper.CreateMap<CDModel, CD>()
        .ForMember(c => c.Name, opt => opt.MapFrom(m => m.Title));

Is there an easy way to retrieve the original expression like c => c.Name (for source) and m => m.Title (for destination) from the mapping?

I tried this, but I miss some things...

var map = Mapper.FindTypeMapFor<CDModel, CD>();
foreach (var propertMap in map.GetPropertyMaps())
{
    var source = ???;
    var dest = propertMap.DestinationProperty.MemberInfo;
}

How to get the source and destination expressions?


Solution

  • Going along the same path as what you were doing ...

    foreach( var propertMap in map.GetPropertyMaps() )
    {
        var dest = propertMap.DestinationProperty.MemberInfo;
        var source = propertMap.SourceMember;
    }
    

    How exactly do you want the expressions? Are you wanting the underlying Lambas?

    If so look at

    propertMap.GetSourceValueResolvers()