Skip to content

Instantly share code, notes, and snippets.

@mbenford
Last active July 7, 2016 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbenford/e507989a79925896abdcf64563fe2c57 to your computer and use it in GitHub Desktop.
Save mbenford/e507989a79925896abdcf64563fe2c57 to your computer and use it in GitHub Desktop.
public static class MappingExtensions
{
public static void Unflatten<T>(this IMemberConfigurationExpression<T> config)
{
config.ResolveUsing(resolutionResult =>
{
var context = resolutionResult.Context;
var prefix = context.MemberName;
var destProperties = context.DestinationType.GetProperties();
var dest = Activator.CreateInstance(context.DestinationType);
foreach (var sourceMember in context.SourceType.GetProperties())
{
var matchedProperty = destProperties.FirstOrDefault(p => sourceMember.Name == prefix + p.Name);
if (matchedProperty != null) matchedProperty.SetValue(dest, sourceMember.GetValue(context.SourceValue));
}
return dest;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment