Last active
December 22, 2023 09:58
-
-
Save pppoe252110/983a5c3a10c201c23e0bdcd85773a9e7 to your computer and use it in GitHub Desktop.
DTO Clone
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
public static class DTO | |
{ | |
public static T GetClone<T>(this object donor) | |
{ | |
var type = typeof(T); | |
var target = Activator.CreateInstance(type); | |
var typeDonor = donor.GetType(); | |
var props = type.GetProperties(); | |
foreach (var prop in props) | |
{ | |
var from = typeDonor.GetProperty(prop.Name); | |
if (type.GetProperty(prop.Name) != null && from != null) | |
{ | |
var value = from.GetValue(donor, null); | |
prop.SetValue(target, value); | |
} | |
} | |
return (T)target; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment