Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- namespace Zad1
- {
- static class Serialzator
- {
- public static void Serialize<T>(string FileName, T obj)
- {
- if (obj is ISerializable)
- {
- FileStream fs = new FileStream(FileName,FileMode.Create);
- BinaryFormatter bf = new BinaryFormatter();
- bf.Serialize(fs, obj);
- fs.Close();
- }
- }
- public static T Deserialize<T>(string FileName)
- {
- FileStream fs = new FileStream(FileName,FileMode.Open);
- BinaryFormatter bf = new BinaryFormatter();
- T obj = (T)bf.Deserialize(fs);
- fs.Close();
- return obj;
- }
- }
- [Serializable]
- class Notatka : IComparable<Notatka>, ISerializable
- {
- string tekst,temat;
- public Notatka()
- {
- tekst = "";
- temat = "";
- }
- public Notatka(string tekst, string temat)
- {
- this.tekst = tekst;
- this.temat = temat;
- }
- public override string ToString()
- {
- return $"Notatka: {temat} treΕΔ: {tekst}";
- }
- public void GetObjectData(SerializationInfo info, StreamingContext context)
- {
- info.AddValue("tekst",tekst);
- info.AddValue("temat",temat);
- }
- public Notatka(SerializationInfo info, StreamingContext context)
- {
- temat = info.GetString("temat");
- tekst = info.GetString("tekst");
- }
- public int CompareTo(Notatka obj)
- {
- if (temat != obj.temat)
- {
- return temat.CompareTo(obj.temat);
- }
- else return tekst.CompareTo(obj.tekst);
- }
- public static bool operator ==(Notatka N1, Notatka N2)
- {
- return N1.CompareTo(N2) == 0 ? true : false;
- }
- public static bool operator !=(Notatka N1, Notatka N2)
- {
- return N1.CompareTo(N2) != 0 ? true : false;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Notatka n1 = new Notatka("bAudi","cPojazd");
- Notatka n2 = new Notatka("aBMW", "cPojazd");
- Notatka n3 = new Notatka("Mercedes", "dPojazd");
- Notatka n4 = new Notatka("Zuk", "ePojazd");
- Notatka n5 = new Notatka("Samsung", "bTelefon");
- Notatka n6 = new Notatka("Audi", "aPojazd");
- List<Notatka> n = new List<Notatka>(){n1,n2,n3,n4,n5,n6};
- foreach (var VARIABLE in n)
- {
- Console.WriteLine(VARIABLE);
- }
- n.Sort();
- Console.WriteLine("------------------");
- foreach (var VARIABLE in n)
- {
- Console.WriteLine(VARIABLE);
- }
- Serialzator.Serialize("xd.bin",n);
- Serialzator.Deserialize<Notatka>("xd.bin");
- foreach (var ZMIENNA in n)
- {
- Console.WriteLine(ZMIENNA);
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement