Reputation: 9830
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?
Upvotes: 15
Views: 11794
Reputation: 320
On AutoMapper 12 you no longer appear to be able to get information on the maps or profiles from IMapper
or from IConfigurationProvider
without using reflection.
If you'd rather avoid reflection, then you can get at least some of the information from the profiles directly by casting them to IProfileConfiguration
.
If you register your profiles with DI like I do, and you just need the types you're mapping to/from, then you can do something along the lines of this to get all source/destination types:
public class TypeMapProvider
{
private readonly IEnumerable<Profile> profiles;
public TypeMapProvider(IEnumerable<Profile> profiles)
{
this.profiles = profiles;
}
public IEnumerable<(Type Source, Type Destination)> GetTypeMaps()
{
foreach (var profile in this.profiles)
{
foreach (var typeMap in ((IProfileConfiguration)profile).TypeMapConfigs)
{
yield return (typeMap.Source, typeMap.Destination);
}
}
}
}
I'm not sure if the IProfileConfiguration.TypeMapConfigs
property (or any of the other properties on that interface) will provide information on the members involved or the expressions, etc., but it's worth checking.
Hopefully this helps someone.
Upvotes: 0
Reputation: 11357
I'm using Automapper 7.0 and the syntax is different now. For example,
void Dump(TypeMap map)
{
Console.WriteLine("---------------------------------------------------------------------");
Console.WriteLine(map.SourceType + " ==> " + map.DestinationType);
foreach (var m in map.GetPropertyMaps())
{
Console.WriteLine(m.SourceMember.Name + " ==> " + m.DestinationProperty.Name);
}
}
And then you can call it using:
Dump(Mapper.Instance.ConfigurationProvider.FindTypeMapFor(typeof(CDModel), typeof(CD)));
or if you want to dump out everything, then do like this.
foreach (var map in Mapper.Instance.ConfigurationProvider.GetAllTypeMaps())
{
Dump(map);
}
Upvotes: 2
Reputation: 12163
I also find var map = Mapper.GetAllTypeMaps();
to be useful too, as you can search for the SourceType or DestinationType.
Upvotes: 2
Reputation: 3523
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()
Upvotes: 12