Advertisement
Danielos168

XdDD

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