public static void CopieTo(I2iSerializable source, I2iSerializable cible, Dictionary <Type, object> tableObjetsPourSerializer) { try { if (source == null) { return; } if (cible == null) { return; } MemoryStream stream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(stream); C2iSerializer serializer = new CSerializerSaveBinaire(writer); CResultAErreur result = source.Serialize(serializer); if (result) { stream.Seek(0, SeekOrigin.Begin); BinaryReader reader = new BinaryReader(stream); serializer = new CSerializerReadBinaire(reader); if (tableObjetsPourSerializer != null) { foreach (KeyValuePair <Type, object> keyVal in tableObjetsPourSerializer) { serializer.AttacheObjet(keyVal.Key, keyVal.Value); } } result = cible.Serialize(serializer); reader.Close(); } writer.Close(); I2iCloneableAvecTraitementApresClonage clone = cible as I2iCloneableAvecTraitementApresClonage; if (clone != null) { clone.TraiteApresClonage(source); } } catch (Exception e) { string strTmp = e.ToString(); Console.WriteLine(I.T("Serializable copy error @1|30043 ", strTmp)); } }
//---------------------------------------------------------------------------------- public static CResultAErreur ReadFromFile(I2iSerializable objet, string strSignatureFichier, string strNomFichier) { CResultAErreur result = CResultAErreur.True; FileStream stream = null; try { stream = new FileStream(strNomFichier, FileMode.Open, FileAccess.Read, FileShare.Read); } catch (Exception e) { result.EmpileErreur(new CErreurException(e)); result.EmpileErreur(I.T("File opening error|30060")); } try { BinaryReader reader = new BinaryReader(stream); string strId = reader.ReadString(); if (strId != strSignatureFichier) { result.EmpileErreur(I.T("The file does not contains a valid structure|30061")); reader.Close(); return(result); } CSerializerReadBinaire serializer = new CSerializerReadBinaire(reader); result = objet.Serialize(serializer); reader.Close(); } catch (Exception e) { result.EmpileErreur(new CErreurException(e)); result.EmpileErreur(I.T("File reading error|30062")); } finally { try { stream.Close(); } catch {} } return(result); }
/// <summary> /// récupère l'objet dans la valise /// Le data du result contient l'objet /// </summary> /// <param name="objetsAttaches">Par couple : type objet, objet attachés aux sérializers</param> /// <returns></returns> public CResultAErreur GetObjet(params object[] objetsAttaches) { I2iSerializable objet = null; MemoryStream stream = new MemoryStream(m_data); BinaryReader reader = new BinaryReader(stream); CSerializerReadBinaire serializer = new CSerializerReadBinaire(reader); for (int nParam = 0; nParam < objetsAttaches.Length; nParam += 2) { serializer.AttacheObjet((Type)objetsAttaches[nParam], objetsAttaches[nParam + 1]); } CResultAErreur result = serializer.TraiteObject(ref objet); if (result) { result.Data = objet; } reader.Close(); stream.Close(); return(result); }