fork download
  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5.  
  6.  
  7. [Serializable] //This is needed to binary serialization
  8. public class Demo
  9. {
  10. public uint X{get;set;}
  11. public uint Y;
  12. public DateTime Z;
  13. public Demo(){}
  14. }
  15.  
  16. public static class BinaryCloner
  17. {
  18. public static T DeepCopy<T>(T obj)
  19. {
  20. using (var memoryStream = new MemoryStream())
  21. {
  22. var formatter = new BinaryFormatter();
  23. formatter.Serialize(memoryStream, obj);
  24. memoryStream.Seek(0, SeekOrigin.Begin);
  25. return (T)formatter.Deserialize(memoryStream);
  26. }
  27. }
  28. }
  29. public class Test
  30. {
  31. public static void Main()
  32. {
  33. Demo a = new Demo();
  34. a.X = 50;
  35. a.Y = 20;
  36. a.Z = DateTime.Now;
  37. Demo b = BinaryCloner.DeepCopy(a);
  38. Console.WriteLine($"{a.X} | {b.X}");
  39. Console.WriteLine($"{a.Y} | {b.Y}");
  40. Console.WriteLine($"{a.Z} | {b.Z}");
  41.  
  42.  
  43. // your code goes here
  44. }
  45. }
  46.  
Success #stdin #stdout 0.09s 32420KB
stdin
Standard input is empty
stdout
50 | 50
20 | 20
8/8/2025 10:46:54 AM | 8/8/2025 10:46:54 AM