Advertisement
halleman19

serialize list object | unity3d

Oct 26th, 2023
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | Gaming | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5.  
  6. namespace xmlapp
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. List<Player> list = new List<Player>();
  13.  
  14. list.Add(new Player(27, "Sam", "Halleman"));
  15. list.Add(new Player(24, "Dairysh", "none"));
  16.  
  17. BinaryFormatter formatter = new BinaryFormatter();
  18.  
  19. byte[] data;
  20. MemoryStream stream = new MemoryStream();
  21.  
  22. formatter.Serialize(stream, list);
  23. data = stream.ToArray();
  24.  
  25. MemoryStream ms = new MemoryStream(data);
  26.  
  27. List<Player> players = (List<Player>)formatter.Deserialize(ms);
  28. }
  29. }
  30.  
  31. [Serializable]
  32. public class Player
  33. {
  34. private int age;
  35. private string name;
  36. [NonSerialized]
  37. private string surname;
  38.  
  39. public int Age
  40. {
  41. get { return this.age; }
  42. }
  43.  
  44. public string Name
  45. {
  46. get { return this.name; }
  47. }
  48.  
  49. public string SurName
  50. {
  51. get { return this.surname; }
  52. }
  53.  
  54. public Player(int age, string name, string surname)
  55. {
  56. this.age = age;
  57. this.name = name;
  58. this.surname = surname;
  59. }
  60. }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement