Skip to content

Instantly share code, notes, and snippets.

@codywilliamson
Created March 9, 2022 18:23
Show Gist options
  • Save codywilliamson/4476177b3f7157ba5034ba561e6ec1f6 to your computer and use it in GitHub Desktop.
Save codywilliamson/4476177b3f7157ba5034ba561e6ec1f6 to your computer and use it in GitHub Desktop.
Map a generic parent object to its child
/// <summary>
/// Maps a parent object to a child object. <br></br>
/// </summary>
/// <typeparam name="T">Generic Type of the Parent Object</typeparam>
/// <typeparam name="U">Generic Type of the Child Object</typeparam>
/// <param name="parent">Parent Object</param>
/// <param name="child">Child Object</param>
/// <returns>U Child object</returns>
public U MapParentToChild<T, U>(T parent, U child)
{
foreach (PropertyInfo prop in parent.GetType().GetProperties())
child.GetType().GetProperty(prop.Name).SetValue(child, parent.GetType().GetProperty(prop.Name).GetValue(parent));
return child;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment