Reputation: 51
How do I get the name of the destination property:
Public class Source{
public string FirstName{ get; set; }
}
public class Destination{
public string C_First_Name{ get; set; }
}
Using AutoMapper, how do i get the name of the destination property when i pass source property Name.
Upvotes: 5
Views: 5519
Reputation: 15982
For some map configuration:
var mapper = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Source, Destination>().ForMember(dst => dst.C_First_Name, opt => opt.MapFrom(src => src.FirstName));
});
You can define a method like this:
public string GetDestinationPropertyFor<TSrc, TDst>(MapperConfiguration mapper, string sourceProperty)
{
var map = mapper.FindTypeMapFor<TSrc, TDst>();
var propertyMap = map.GetPropertyMaps().First(pm => pm.SourceMember == typeof(TSrc).GetProperty(sourceProperty));
return propertyMap.DestinationProperty.Name;
}
Then use it like so:
var destinationName = GetDestinationPropertyFor<Source, Destination>(mapper, "FirstName");
Upvotes: 11