static void Main(string[] args)
        {
            Random rand = new Random();

            ConsoleSymbolStruct[] simbols = new ConsoleSymbolStruct[rand.Next(3, 10)];
            for (int i = 0; i < simbols.Length; i++)
            {
                simbols[i] = new ConsoleSymbolStruct(rand.Next(-20, 21), rand.Next(-20, 21), (char)rand.Next(33, 127));
            }
            Console.WriteLine("Изначальный массив:");
            Array.ForEach(simbols, (x) => Console.WriteLine(x));
            BinaryFormatter bf = new BinaryFormatter();

            using (FileStream fs = new FileStream("1.txt", FileMode.OpenOrCreate))
            {
                bf.Serialize(fs, simbols);
            }
            using (FileStream fs = new FileStream("1.txt", FileMode.OpenOrCreate))
            {
                ConsoleSymbolStruct[] newSimbols = (ConsoleSymbolStruct[])bf.Deserialize(fs);
                Console.WriteLine("Бинарная десериализация:");
                Array.ForEach(newSimbols, (x) => Console.WriteLine(x));
            }
            XmlSerializer x = new XmlSerializer(typeof(ConsoleSymbolStruct[]));

            using (FileStream fs = new FileStream("2.xml", FileMode.OpenOrCreate))
            {
                x.Serialize(fs, simbols);
            }
            using (FileStream fs = new FileStream("2.xml", FileMode.OpenOrCreate))
            {
                ConsoleSymbolStruct[] newSimbols = (ConsoleSymbolStruct[])x.Deserialize(fs);
                Console.WriteLine("Xml-десериализация:");
                Array.ForEach(newSimbols, (x) => Console.WriteLine(x));
            }
            string json = JsonSerializer.Serialize(simbols);

            ConsoleSymbolStruct[] jsonSymbols = JsonSerializer.Deserialize <ConsoleSymbolStruct[]>(json);
            Console.WriteLine("Json-десериализация:");
            Array.ForEach(jsonSymbols, (x) => Console.WriteLine(x));
            var ds = new DataContractSerializer(typeof(ConsoleSymbolStruct[]));

            using (FileStream fs = new FileStream("3.xml", FileMode.OpenOrCreate))
            {
                ds.WriteObject(fs, simbols);
            }
            using (FileStream fs = new FileStream("3.xml", FileMode.OpenOrCreate))
            {
                ConsoleSymbolStruct[] newSimbols = (ConsoleSymbolStruct[])ds.ReadObject(fs);
                Console.WriteLine("DataContract-десериализация:");
                Array.ForEach(newSimbols, (x) => Console.WriteLine(x));
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            ConsoleColor[]        colors = new ConsoleColor[] { ConsoleColor.Blue, ConsoleColor.Red, ConsoleColor.Green, ConsoleColor.Cyan, ConsoleColor.Magenta, ConsoleColor.Yellow };
            ConsoleSymbolStruct[] arr    = new ConsoleSymbolStruct[10];
            for (int i = 0; i < arr.Length; ++i)
            {
                arr[i] = new ColoredConsoleSymbol((char)rnd.Next('a', 'z'), rnd.Next(1, 15), rnd.Next(1, 15), colors[rnd.Next(0, colors.Length)]);
            }

            PrintChars(arr);

            Thread.Sleep(200);
            Console.Clear();
            Console.WriteLine("Binary: ");
            BinaryFormatter bf = new BinaryFormatter();

            using (Stream s = File.OpenWrite("bin"))
            {
                bf.Serialize(s, arr);
            }
            using (Stream s = File.OpenRead("bin"))
            {
                ConsoleSymbolStruct[] arr2 = (ConsoleSymbolStruct[])bf.Deserialize(s);
                PrintChars(arr2);
            }

            Thread.Sleep(200);
            Console.Clear();
            Console.WriteLine("Xml: ");
            XmlSerializer xs = new XmlSerializer(typeof(ConsoleSymbolStruct[]), new Type[] { typeof(ColoredConsoleSymbol) });

            using (Stream s = File.OpenWrite("xml"))
            {
                xs.Serialize(s, arr);
            }
            using (Stream s = File.OpenRead("xml"))
            {
                ConsoleSymbolStruct[] arr3 = (ConsoleSymbolStruct[])xs.Deserialize(s);
                PrintChars(arr3);
            }

            Thread.Sleep(200);
            Console.Clear();
            Console.WriteLine("Json: ");
            File.WriteAllText("json", JsonSerializer.Serialize(arr, typeof(ConsoleSymbolStruct[])));
            using (StreamReader sr = new StreamReader(File.OpenRead("json")))
            {
                ConsoleSymbolStruct[] arr4 = (ConsoleSymbolStruct[])JsonSerializer.Deserialize <ConsoleSymbolStruct[]>(sr.ReadLine());
                PrintChars(arr4);
            }

            Thread.Sleep(200);
            Console.Clear();
            Console.WriteLine("DataContract: ");
            DataContractSerializer ds = new DataContractSerializer(typeof(ConsoleSymbolStruct[]), new Type[] { typeof(ColoredConsoleSymbol) });

            using (Stream s = File.OpenWrite("data"))
            {
                ds.WriteObject(s, arr);
            }
            using (Stream s = File.OpenRead("data"))
            {
                ConsoleSymbolStruct[] arr5 = (ConsoleSymbolStruct[])ds.ReadObject(s);
                PrintChars(arr5);
            }
        }