Beispiel #1
0
        public InfoSupplChat infoSuppl;  //Pour test Serialisation/Deserialisation d'un membre de type instance.


        public Chat(int poids, InfoSupplChat infoSuppl)
        {
            this.poids     = poids;
            this.infoSuppl = infoSuppl;

            this.noms.Add("NomChat");
            this.noms.Add("PseudoChat");
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            Chien         monChien       = new Chien();
            InfoSupplChat infoSupplChat1 = new InfoSupplChat();

            infoSupplChat1.infoSpecial1 = "Chat1 gentil!!";
            InfoSupplChat infoSupplChat2 = new InfoSupplChat();

            infoSupplChat2.infoSpecial1 = "Chat2 pas sympa!!";
            Chat monChat1 = new Chat(12, infoSupplChat1);
            Chat monChat2 = new Chat(14, infoSupplChat2);

            List <IAnimal> animaux = new List <IAnimal>()
            {
                monChien, monChat1
            };

            animaux.Add(monChat2);

            //SERIALISATION
            string animauxToString = JsonConvert.SerializeObject(animaux);         //J'ai installé pour ça le Nugget NewonSoft.Json

            Console.WriteLine("List<IAnimal> sérialisée: \n\n" + animauxToString); //OK
            Console.ReadKey();


            //DESERIALISATION : ci-dessous, CAST IMPOSSIBLE de la caîne sérialisée, en List<IAnimal>, car IAnimal étant un type abstrait
            //                  il ne peut être instancié !!!
            //List<IAnimal> animaux2 = JsonConvert.DeserializeObject<List<IAnimal>>(animauxToString); //J'ai installé pour ça le Nugget NewonSoft.Json

            List <Chat> chats = new List <Chat>()
            {
                monChat1, monChat2
            };
            string chatsToString = JsonConvert.SerializeObject(chats); //J'ai installé pour ça le Nugget NewonSoft.Json

            Console.WriteLine("\n\n\n\n");
            Console.WriteLine("List<Chat> sérialisée: \n\n" + chatsToString + "\n\n\n"); //OK

            Console.WriteLine("LECTURE List<Chat> Désérialisée :\n\n");
            List <Chat> chats2 = JsonConvert.DeserializeObject <List <Chat> >(chatsToString); //J'ai installé pour ça le Nugget NewonSoft.Json

            foreach (Chat chat in chats2)
            {
                Console.WriteLine(chat.poids);
                chat.crier();
                chat.allerALaLitiere();
                Console.WriteLine("chat.getInfoSuppl().infoSpecial1: " + chat.getInfoSuppl().infoSpecial1);
                Console.WriteLine("chat.getInfoSuppl().infoSpecial2: " + chat.getInfoSuppl().infoSpecial2);
                Console.WriteLine("\n\n");
            }
            ;

            Console.WriteLine("OK");
            Console.ReadKey();
        }