Created
July 8, 2017 04:57
-
-
Save jpdillingham/7b09788d534c868dfa2701383df06fbd to your computer and use it in GitHub Desktop.
MapFrom<Tderived, Tbase>
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 the values of the public properties of the specified base object to the specified derived object's properties. | |
/// </summary> | |
/// <typeparam name="Tderived">The Type of the derived class.</typeparam> | |
/// <typeparam name="Tbase">The Type of the base class.</typeparam> | |
/// <param name="derivedObject">The instance of the derived class into which values are to be mapped.</param> | |
/// <param name="baseObject">The instance of the base class from whihc values are to be copied.</param> | |
public static void MapFrom<Tderived, Tbase>(this Tderived derivedObject, Tbase baseObject) | |
where Tderived : Tbase | |
{ | |
PropertyInfo[] properties = typeof(Tbase).GetProperties(BindingFlags.Instance | BindingFlags.Public); | |
foreach (PropertyInfo property in properties) | |
{ | |
typeof(Tderived).GetProperty(property.Name).SetValue(derivedObject, property.GetValue(baseObject)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment