Created
March 9, 2022 18:23
-
-
Save codywilliamson/4476177b3f7157ba5034ba561e6ec1f6 to your computer and use it in GitHub Desktop.
Map a generic parent object to its child
This file contains 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
/// <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