Created
December 27, 2018 04:19
-
-
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); ```
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 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