Beispiel #1
0
        static void Main(string[] args)
        {
            University university = new University {
                Name = "HSE"
            };
            Human h1 = new Human {
                Name = "Karya"
            };
            Human h2 = new Human {
                Name = "Tenya"
            };
            Department dep = new Department {
                Name = "FCS"
            };

            dep.humen.Add(h1);
            dep.humen.Add(h2);
            university.departments.Add(dep);


            FileStream      bas    = new FileStream("../../university.ser", FileMode.OpenOrCreate);
            BinaryFormatter format = new BinaryFormatter();

            format.Serialize(bas, university);
            bas.Close();

            bas = new FileStream("../../university.ser", FileMode.Open);



            University sharaga = (University)format.Deserialize(bas);

            Console.WriteLine(sharaga.ToString());



            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            University HSE = new University();

            HSE.UniversityName = "NRU HSE";

            Human[] deptStaff = { new Professor("Ivanov"),
                                  new Professor("Petrov") };

            Dept SE = new Dept("SE", deptStaff);

            HSE.Departments = new List <Dept>();
            HSE.Departments.Add(SE);

            University MSU = new University();

            MSU.UniversityName = "MSU";

            Human[] deptStaffM = { new Professor("Ivanov"), new Professor("Chizov"),
                                   new Professor("Petrov") };

            Dept SEM = new Dept("SEM", deptStaffM);

            MSU.Departments = new List <Dept>();
            MSU.Departments.Add(SEM);

            University[] universities = new University[] { HSE, MSU };

            DataContractSerializer contracFormatter = new DataContractSerializer(typeof(University[]), new Type[] { typeof(University), typeof(Professor), typeof(Dept), typeof(Human) });

            // Сериализация
            using (Stream file = new FileStream("JsonSer.json", FileMode.Create, FileAccess.Write, FileShare.None))
            {
                contracFormatter.WriteObject(file, universities);
            }

            // Десериализация
            University[] deserial;
            using (Stream file = File.OpenRead("JsonSer.json"))
            {
                deserial = (University[])contracFormatter.ReadObject(file);
                Console.WriteLine("JSON - " + deserial[0].UniversityName);
                Console.WriteLine("JSON - " + deserial[1].UniversityName);
            }

            foreach (Dept d in deserial[0].Departments)
            {
                foreach (Human h in d.Staff)
                {
                    if (h is Professor)
                    {
                        Console.WriteLine(d.DeptName + " prof.: " + h.Name);
                    }
                }
            }

            foreach (Dept d in deserial[1].Departments)
            {
                foreach (Human h in d.Staff)
                {
                    if (h is Professor)
                    {
                        Console.WriteLine(d.DeptName + " prof.: " + h.Name);
                    }
                }
            }

            Console.ReadKey();
        }