Skip to content

Instantly share code, notes, and snippets.

@pppoe252110
Last active December 22, 2023 09:58
Show Gist options
  • Save pppoe252110/983a5c3a10c201c23e0bdcd85773a9e7 to your computer and use it in GitHub Desktop.
Save pppoe252110/983a5c3a10c201c23e0bdcd85773a9e7 to your computer and use it in GitHub Desktop.
DTO Clone
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