Skip to content

Instantly share code, notes, and snippets.

@Plus1XP
Last active April 16, 2019 09:43
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 Plus1XP/df605915805e510a9d714583f7dfbe94 to your computer and use it in GitHub Desktop.
Save Plus1XP/df605915805e510a9d714583f7dfbe94 to your computer and use it in GitHub Desktop.
Copies Fields & Properties from one class to another
static class CopyObject
{
public static void ShallowCopy(this Object dst, object src)
{
var srcT = src.GetType();
var dstT = dst.GetType();
foreach (var f in srcT.GetFields())
{
var dstF = dstT.GetField(f.Name);
if (dstF == null)
continue;
dstF.SetValue(dst, f.GetValue(src));
}
foreach (var f in srcT.GetProperties())
{
var dstF = dstT.GetProperty(f.Name);
if (dstF == null)
continue;
dstF.SetValue(dst, f.GetValue(src, null), null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment