Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Désérialisation des types génériques

Non,Edité
{filelink=17488}

    using System;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;
 
    [Serializable]
    public class GeneriqueSerialisation<T>
    {
        public GeneriqueSerialisation(T init)
        {
            champ=init;
        }
 
        public void GetObjectData(SerializationInfo info,StreamingContext ctx)
        {
            info.AddValue("champ", champ, typeof(T));
        }
 
        private GeneriqueSerialisation(SerializationInfo info, StreamingContext ctx)
        {
            champ=(T) info.GetValue("champ", typeof(T));
        }
        public void SetValue(T data) {
           champ=data;
        }
        public T GetValue() {
           return champ;
        }
        private T champ=default(T);
    }
 
    public class Utilisation
    {
        public static void Main(string [] args)
        {
            BinaryFormatter binaire=new BinaryFormatter();
            FileStream fichier= new FileStream("teste.dat", FileMode.OpenOrCreate);
 
            GeneriqueSerialisation<int> obj1=(GeneriqueSerialisation<int>)
            binaire.Deserialize(fichier);
            Console.WriteLine(obj1.GetValue());
        }
    }

No comments yet.

Leave a Comment