Created
June 12, 2018 19:08
-
-
Save LSViana/0f6582492e6f52c4556c92f20c7aacf1 to your computer and use it in GitHub Desktop.
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
public static void Copy(this Object destiny, Object origin, IEnumerable<string> fieldsToIgnore) | |
{ | |
fieldsToIgnore = fieldsToIgnore.ToArray(); | |
// | |
var typeDestiny = destiny.GetType(); | |
var propsDestiny = typeDestiny.GetProperties(); | |
var typeOrigin = origin.GetType(); | |
var propsOrigin = typeOrigin.GetProperties(); | |
foreach (var prop in propsDestiny) | |
{ | |
if (prop.SetMethod == null) | |
continue; | |
var propOrigin = propsOrigin.FirstOrDefault(a => a.Name == prop.Name); | |
if (propOrigin is null) | |
continue; | |
if (fieldsToIgnore.Contains(prop.Name)) | |
continue; | |
prop.SetValue(destiny, prop.GetValue(origin)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment