Advertisement
ForeverZer0

Binary/XML Serializer

May 23rd, 2014
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.53 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization.Formatters.Binary;
  4. using System.Xml.Serialization;
  5.  
  6. namespace MyNamespace
  7. {
  8.     /// <summary>
  9.     /// Static class for serializing and deserializing objects
  10.     /// </summary>
  11.     public static class Serializer
  12.     {
  13.         /// <summary>
  14.         /// Saves an object to disk in binary format
  15.         /// </summary>
  16.         /// <typeparam name="T">Type of object that will be saved</typeparam>
  17.         /// <param name="data">Object to be saved</param>
  18.         /// <param name="path">Path where object will be saved</param>
  19.         /// <param name="suppress">Flag indicating if exception will be caught if error occurs</param>
  20.         /// <returns>Flag indicating if save was successful or not</returns>
  21.         public static bool SaveBinary<T>(T data, string path, bool suppress = true)
  22.         {
  23.             var binFormatter = new BinaryFormatter();
  24.             try
  25.             {
  26.                 using (var stream = File.OpenWrite(path))
  27.                     binFormatter.Serialize(stream, data);
  28.                 return true;
  29.             }
  30.             catch (Exception error)
  31.             {
  32.                 if (suppress)
  33.                     return false;
  34.                 throw error;
  35.             }
  36.         }
  37.  
  38.         /// <summary>
  39.         /// Saves an object to disk in XML format
  40.         /// </summary>
  41.         /// <typeparam name="T">Type of object that will be saved</typeparam>
  42.         /// <param name="data">Object to be saved</param>
  43.         /// <param name="path">Path where object will be saved</param>
  44.         /// <param name="suppress">Flag indicating if exception will be caught if error occurs</param>
  45.         /// <returns>Flag indicating if save was successful or not</returns>
  46.         public static bool SaveXML<T>(T data, string path, bool suppress = true)
  47.         {
  48.             try
  49.             {
  50.                 var serializer = new XmlSerializer(typeof(T));
  51.                 using (TextWriter writerStream = new StreamWriter(path))
  52.                     serializer.Serialize(writerStream, data);
  53.                 return true;
  54.             }
  55.             catch (Exception error)
  56.             {
  57.                 if (suppress)
  58.                     return false;
  59.                 throw error;
  60.             }
  61.         }
  62.  
  63.         /// <summary>
  64.         /// Loads a previously binary serialized object and returns it
  65.         /// </summary>
  66.         /// <typeparam name="T">Type of object that will be loaded</typeparam>
  67.         /// <param name="path">Path where object to load from</param>
  68.         /// <param name="suppress">Flag indicating if default instance of object will be returned if exception occurs</param>
  69.         /// <returns>The deserialized object, or new instance of that type if error occured</returns>
  70.         public static T LoadBinary<T>(string path, bool suppress = true)
  71.         {
  72.             var binFormatter = new BinaryFormatter();
  73.             try
  74.             {
  75.                 T data;
  76.                 using (var stream = File.OpenRead(path))
  77.                     data = (T)binFormatter.Deserialize(stream);
  78.                 return data;
  79.  
  80.             }
  81.             catch (Exception error)
  82.             {
  83.                 if (suppress)
  84.                     return Activator.CreateInstance<T>();
  85.                 throw error;
  86.             }
  87.         }
  88.  
  89.         /// <summary>
  90.         /// Loads a previously XML serialized object and returns it
  91.         /// </summary>
  92.         /// <typeparam name="T">Type of object that will be loaded</typeparam>
  93.         /// <param name="path">Path where object to load from</param>
  94.         /// <param name="suppress">Flag indicating if default instance of object will be returned if exception occurs</param>
  95.         /// <returns>The deserialized object, or new instance of that type if error occured</returns>
  96.         public static T LoadXML<T>(string path, bool suppress = true)
  97.         {
  98.             try
  99.             {
  100.  
  101.                 var serializer = new XmlSerializer(typeof(T));
  102.                 using (TextReader readerStream = new StreamReader(path))
  103.                     return (T)serializer.Deserialize(readerStream);
  104.             }
  105.             catch (Exception error)
  106.             {
  107.                 if (suppress)
  108.                     return Activator.CreateInstance<T>();
  109.                 throw error;
  110.             }
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement