Skip to content

Instantly share code, notes, and snippets.

@LSViana
Created June 12, 2018 19:08
Show Gist options
  • Save LSViana/0f6582492e6f52c4556c92f20c7aacf1 to your computer and use it in GitHub Desktop.
Save LSViana/0f6582492e6f52c4556c92f20c7aacf1 to your computer and use it in GitHub Desktop.
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