Advertisement
halleman19

[de]serialize object | unity3d

Oct 26th, 2023
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | Gaming | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization.Formatters.Binary;
  4.  
  5. namespace xmlapp
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Player pl0 = new Player(27, "Sam", "Halleman");
  12.             Player pl1 = new Player(24, "Dairysh", "none");
  13.  
  14.             BinaryFormatter formatter = new BinaryFormatter();
  15.  
  16.             byte[] data;
  17.             MemoryStream stream = new MemoryStream();
  18.  
  19.             formatter.Serialize(stream, pl0);
  20.             formatter.Serialize(stream, pl1);
  21.  
  22.             data = stream.ToArray();
  23.  
  24.             MemoryStream ms = new MemoryStream(data);
  25.  
  26.             Player pl0ds = (Player)formatter.Deserialize(ms);
  27.             Player pl1ds = (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