Last active
April 16, 2019 09:43
-
-
Save Plus1XP/df605915805e510a9d714583f7dfbe94 to your computer and use it in GitHub Desktop.
Copies Fields & Properties from one class to another
This file contains hidden or 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
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