At application startup we create a new MapperConfiguration and configure it to use our profiles. Inside some of the profiles we use custom ValueResolver's to do data conversion.
With the configuration completed, we can execute something like this to retrieve all of the mapped properties and types.
var mapping = configuration.GetAllTypeMaps().FirstOrDefault();
if (!mapping.IsMapped())
return;
var propertyInfo = mapping.SourceMember as PropertyInfo;
if (propertyInfo == null)
return;
var sourceType = mapping.SourceType;
var destinationType = mapping.DestimationType;
var entry = new TranslationEntry(sourceType, destinationType);
entry.DestinationProperty = mapping.DestinationProperty.Name;
entry.DestinationPropertyType = mapping.DestinationPropertyType;
entry.SourceProperty = propertyInfo.Name;
entry.SourcePropertyType = propertyInfo.PropertyType;
In this example I'm only getting the first mapping, but this is just for example purposes. The problem I'm having is determining the custom resolver used in a particular mapping.
I've found that any mapping that has a custom resolver seems to have a value resolver of DelegateBasedResolver<>
var resolver = (from a in mapping.GetSourceValueResolvers()
let b = a.GetType()
where b.IsGenericType
&& b.GetGenericTypeDefinition() == typeof(DelegateBasedResolver<>)
select a).FirstOrDefault();
This seems to work and whenever a custom resolver is connected to the mapping resolver is not null. But this is as far as I've gotten.
Let's say I have a simple mapping like this:
public class ExampleProfile : Profile
{
protected override void Configure()
{
CreateMap<ExampleDto, ExampleSummary>()
.ForMember(a => a.Id, a => a.MapFrom(b => b.ExampleId))
.ForMember(a => a.IsEnabled, a => a.ResolveUsing<CustomStringToBooleanResolver>().FromMember(b => b.is_enabled));
}
}
What I'm trying to get from the mapping above, is either the type CustomStringToBooleanResolver or an instance of it. I think I know how to execute the resolver, but that's not what I want to do. Anyone have any ideas?
Aucun commentaire:
Enregistrer un commentaire