I am working on a helper method that will map properties from an ExpandoObject
to a user supplied object and was wondering if the code could be cleaned up or made any more efficiently. It currently has the correct behaviour from a simple test.
public static class Mapper
{
public static void Map<T>(ExpandoObject source, T destination)
{
IDictionary<string, object> dict = source;
var type = destination.GetType();
foreach (var prop in type.GetProperties())
{
var lower = prop.Name.ToLower();
var key = dict.Keys.SingleOrDefault(k => k.ToLower() == lower);
if (key != null)
{
prop.SetValue(destination, dict[key], null);
}
}
}
}
Full test can be seen here. There is currently no type checking. Would this be next to add?