/ Published in: C#

Copy from one object to another of the same type
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
public static void ShallowCopy(Object dest, Object src) { BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; FieldInfo[] destFields = dest.GetType().GetFields(flags); FieldInfo[] srcFields = src.GetType().GetFields(flags); foreach (FieldInfo srcField in srcFields) { FieldInfo destField = destFields.FirstOrDefault(field => field.Name == srcField.Name); if (destField != null && !destField.IsLiteral) { if (srcField.FieldType == destField.FieldType) destField.SetValue(dest, srcField.GetValue(src)); } } }
Comments
