Skip to content

Instantly share code, notes, and snippets.

@longtth
Created December 27, 2018 04:19
Show Gist options
  • Select an option

  • Save longtth/cc4fcf0619917d44e6dbc6ec85b66f45 to your computer and use it in GitHub Desktop.

Select an option

Save longtth/cc4fcf0619917d44e6dbc6ec85b66f45 to your computer and use it in GitHub Desktop.
để làm được như này ```cs MyObject myObj = new MyObject(); // Create and fill a new object MyObject newObj = CloneObject(myObj); ```
public static object CloneObject(object o)
{
Type t = o.GetType();
PropertyInfo[] properties = t.GetProperties();
Object p = t.InvokeMember("", System.Reflection.BindingFlags.CreateInstance,
null, o, null);
foreach (PropertyInfo pi in properties)
{
if (pi.CanWrite)
{
pi.SetValue(p, pi.GetValue(o, null), null);
}
}
return p;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment