Skip to content

Instantly share code, notes, and snippets.

@tluyben
Created April 20, 2020 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tluyben/72cefeea3b9227adc1d8123026e353b1 to your computer and use it in GitHub Desktop.
Save tluyben/72cefeea3b9227adc1d8123026e353b1 to your computer and use it in GitHub Desktop.
/// <summary>
/// Copy every matching property from source -> target
/// </summary>
/// <param name="target"></param>
/// <param name="source"></param>
public static void Slurp(this object target, object source)
{
target.GetType()
.GetProperties()
.Where(p => p.SetMethod != null)
.ToList()
.ForEach(p =>
{
var ps = source.GetType().GetProperty(p.Name);
if (ps != null && ps.GetMethod!=null)
{
p.SetValue(target, ps.GetValue(source));
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment