Skip to content

Instantly share code, notes, and snippets.

@lbargaoanu
Created September 10, 2015 11:08
Show Gist options
  • Save lbargaoanu/8cc2a4a7dd0e35b8ef21 to your computer and use it in GitHub Desktop.
Save lbargaoanu/8cc2a4a7dd0e35b8ef21 to your computer and use it in GitHub Desktop.
Map dynamic to object
using StringDictionary = IDictionary<string, object>;
Mapper.CreateMap<StringDictionary, Destination>().AfterMap(AfterMap);
dynamic source = new ExpandoObject();
source.Foo = "Foo";
source.Bar = "Bar";
var destination = new Destination();
Mapper.Map<StringDictionary, Destination>(source, destination);
Debug.Assert(destination.Foo == "Foo");
Debug.Assert(destination.Bar == "Bar");
public class Destination
{
public string Foo { get; set; }
public string Bar { get; set; }
}
public static void AfterMap(StringDictionary dictionary, Destination destination)
{
foreach(var property in dictionary.Keys.Select(name => typeof(Destination).GetProperty(name)))
{
property.SetValue(destination, dictionary[property.Name]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment