ToolCopy.cs 953 B

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using System.IO;
  7. using System.Reflection;
  8. namespace Ips.Library.DxpLib
  9. {
  10. public class ToolCopy
  11. {
  12. //public static object DeepCopy(object obj)
  13. //{
  14. // BinaryFormatter formartter = new BinaryFormatter();
  15. // MemoryStream ms = new MemoryStream();
  16. // formartter.Serialize(ms, obj);
  17. // ms.Position = 0;
  18. // var newObj = formartter.Deserialize(ms);
  19. // return newObj;
  20. //}
  21. public static T DeepCopy<T>(T obj)
  22. {
  23. return (T)DeepCopy(obj);
  24. }
  25. public static T ShallowCopy<T>(T obj)
  26. {
  27. MethodInfo method = obj.GetType().GetMethod("MemberwiseClone", BindingFlags.NonPublic | BindingFlags.Instance);
  28. return (T)method.Invoke(obj, null);
  29. }
  30. }
  31. }