0

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

mamift
  • 651
  • 7
  • 18
  • That's not supported. You can do it in two steps. First from dynamic to an identical typed object and then to the final destination object, when you apply your configuration. – Lucian Bargaoanu Mar 20 '20 at 04:44
  • The motto for dynamic types should be *"creating 3 more problems for every problem it solves"* – TheGeneral Mar 20 '20 at 04:58

0 Answers0