AgileMapper matches flattened member names as you'd expect, but it also has a dedicated .Flatten() API which flattens objects in various ways. It is accessible:

  • Via the static API, using Mapper.Flatten(myObject)
  • Via the instance API, using myInstanceMapper.Flatten(myObject)
  • Via an extension method, using myObject.Flatten()

Flattening produces an object including all the source object's string or value-type members. For example:

var customer = new Customer
{
    Name = "Mrs Customer",
    Dob = new DateTime(1985, 11, 05),
    Address = new Address
    {
        Line1 = "1 Street",
        Postcode = "XY3 8HW"
    }
};

...can be flattened to a Dictionary using:

var flat = Mapper.Flatten(myObject).ToDictionary();
// flat is a Dictionary<string, object> containing:
// ["Name"] = "Mrs Customer"
// ["Dob"] = * DateTime 1985/11/05 *
// ["Address.Line1"] = "1 Street"
// ["Address.Postcode"] = "XY3 8HW"

...or a dynamic using:

dynamic flat = myInstanceMapper.Flatten(myObject).ToDynamic();
// flat is an ExpandoObject with members:
// flat.Name = "Mrs Customer"
// flat.Dob = * DateTime 1985/11/05 *
// flat.Address_Line1 = "1 Street"
// flat.Address_Postcode = "XY3 8HW"

...or a query string-formatted string using:

var flat = myObject.Flatten().ToQueryString();
// flat is a query-string-formatted string with:
//  Name=Mrs%20Customer
// &Dob=11%2F5%2F1985%2012%3A00%3A00%20AM
// &Address%2ELine1=1%20Street
// &Address%2EPostcode=XY3%208HW