Created
September 10, 2015 11:08
-
-
Save lbargaoanu/8cc2a4a7dd0e35b8ef21 to your computer and use it in GitHub Desktop.
Map dynamic to object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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