Advertisement
Danielos168

Untitled

Jun 3rd, 2019
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Runtime.Serialization;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9.  
  10. namespace Zad1
  11. {
  12.     static class Serialzator
  13.     {
  14.         public static void Serialize<T>(string FileName, T obj)
  15.         {
  16.             if (obj is ISerializable)
  17.             {
  18.                 FileStream fs = new FileStream(FileName,FileMode.Create);
  19.                 BinaryFormatter bf = new BinaryFormatter();
  20.                 bf.Serialize(fs, obj);
  21.                 fs.Close();
  22.             }
  23.  
  24.         }
  25.  
  26.  
  27.         public static T Deserialize<T>(string FileName)
  28.         {
  29.             FileStream fs = new FileStream(FileName,FileMode.Open);
  30.  
  31.             BinaryFormatter bf = new BinaryFormatter();
  32.  
  33.             T obj = (T)bf.Deserialize(fs);
  34.  
  35.             fs.Close();
  36.  
  37.             return obj;
  38.         }
  39.     }
  40.     [Serializable]
  41.     class Notatka : IComparable<Notatka>, ISerializable
  42.     {
  43.         string tekst,temat;
  44.  
  45.         public Notatka()
  46.         {
  47.             tekst = "";
  48.             temat = "";
  49.         }
  50.  
  51.         public Notatka(string tekst, string temat)
  52.         {
  53.             this.tekst = tekst;
  54.             this.temat = temat;
  55.         }
  56.  
  57.         public override string ToString()
  58.         {
  59.             return $"Notatka: {temat} treΕ›Δ‡: {tekst}";
  60.         }
  61.  
  62.         public void GetObjectData(SerializationInfo info, StreamingContext context)
  63.         {
  64.             info.AddValue("tekst",tekst);
  65.             info.AddValue("temat",temat);
  66.         }
  67.  
  68.         public Notatka(SerializationInfo info, StreamingContext context)
  69.         {
  70.             temat = info.GetString("temat");
  71.             tekst = info.GetString("tekst");
  72.         }
  73.  
  74.         public int CompareTo(Notatka obj)
  75.         {
  76.             if (temat != obj.temat)
  77.             {
  78.                 return temat.CompareTo(obj.temat);
  79.             }
  80.             else return tekst.CompareTo(obj.tekst);
  81.         }
  82.  
  83.         public static bool operator ==(Notatka N1, Notatka N2)
  84.         {
  85.             return N1.CompareTo(N2) == 0 ? true : false;
  86.         }
  87.         public static bool operator !=(Notatka N1, Notatka N2)
  88.         {
  89.             return N1.CompareTo(N2) != 0 ? true : false;
  90.         }
  91.  
  92.        
  93.     }
  94.     class Program
  95.     {
  96.         static void Main(string[] args)
  97.         {
  98.             Notatka n1 = new Notatka("bAudi","cPojazd");
  99.             Notatka n2 = new Notatka("aBMW", "cPojazd");
  100.             Notatka n3 = new Notatka("Mercedes", "dPojazd");
  101.             Notatka n4 = new Notatka("Zuk", "ePojazd");
  102.             Notatka n5 = new Notatka("Samsung", "bTelefon");
  103.             Notatka n6 = new Notatka("Audi", "aPojazd");
  104.  
  105.             List<Notatka> n = new List<Notatka>(){n1,n2,n3,n4,n5,n6};
  106.  
  107.             foreach (var VARIABLE in n)
  108.             {
  109.                 Console.WriteLine(VARIABLE);
  110.             }
  111.             n.Sort();
  112.             Console.WriteLine("------------------");
  113.             foreach (var VARIABLE in n)
  114.             {
  115.                 Console.WriteLine(VARIABLE);
  116.             }
  117.             Serialzator.Serialize("xd.bin",n);
  118.  
  119.  
  120.             Serialzator.Deserialize<Notatka>("xd.bin");
  121.  
  122.             foreach (var ZMIENNA in n)
  123.             {
  124.                 Console.WriteLine(ZMIENNA);
  125.             }
  126.             Console.ReadKey();
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement