Overblog Suivre ce blog
Editer l'article Administration Créer mon blog

Présentation

  • : Romagny13 - Du .NET,du pur .NET
  • Romagny13 - Du .NET,du pur .NET
  • : Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
  • Contact

Recherche

Articles Récents

28 mai 2007 1 28 /05 /mai /2007 21:32
Classe de sérialization générique
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Cs2PredicateHelper
{
    ///<summary>
    /// Classe de sérialization générique
    ///</summary>
    ///<typeparam name="T">Type générique</typeparam>
    public class SerializeManager<T>
    {
        public static void Serialize(T obj,string Path)
        {
            System.Xml.Serialization.XmlSerializer oXmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
            System.IO.StreamWriter oStreamWriter = new System.IO.StreamWriter(Path);
            oXmlSerializer.Serialize(oStreamWriter, obj);
            oStreamWriter.Close();
        }
        public static T Deserialize(string Path)
        {
            System.Xml.Serialization.XmlSerializer oXmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
            System.IO.StreamReader oStreamReader = new System.IO.StreamReader(Path);
            T obj = (T)oXmlSerializer.Deserialize(oStreamReader);
            oStreamReader.Close();
            return obj;
        }
    }
}
 
Utilisation
           // Serialization
            SerializeManager<List<Contact>>.Serialize(oContacts, "c:/test.xml");
 
            // Deserialization
           List<Contact> c= SerializeManager<List<Contact>>.Deserialize("c:/test.xml");
           dataGridView1.DataSource = c;
 

Partager cet article

Repost 0
Published by Romagny13 - dans C 1.0- 2.0- 3.0
commenter cet article

commentaires