AutoMapper: how to map from dynamic to <T> using property name replacer

597 views Asked by At

I'm trying to map from a dynamic object to a class type, using AutoMapper, whereby the dynamic object has underscores in field names and the destination type class has properties that are spelled the same, but without the underscores.

The source dynamic object looks like:

new ExpandoObject { CENSUS_YR = 2001, BIRTH_DATE = '1998-10-02' }

The destination typed class looks like:

new TDest { CENSUSYR, BIRTH_DATE }

I've tried the following but it doesn't seem to work:

var mapperConfig = new MapperConfiguration(cfg => {
    cfg.ReplaceMemberName("_", "");
    // also tried this
    cfg.AddMemberConfiguration().AddName<ReplaceName>(name => name.AddReplace("_", ""));
});
mapperConfig.AssertConfigurationIsValid();
var mapper = mapperConfig.CreateMapper();
var mappedRecords = dynamicRecordsSource.Select(r => mapper.Map<TDest>(r)).ToArray();

The above code results in all the items in the mappedRecords array to have all null or default property values. I've also tried various overloads of the .Map() method, but still nothing. I suspect there's some basic configuration step I'm missing. The official documentation doesn't provide many examples beyond just basic dynamic object mapping

0

There are 0 answers