Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Xml.Serialization;
- namespace MyNamespace
- {
- /// <summary>
- /// Static class for serializing and deserializing objects
- /// </summary>
- public static class Serializer
- {
- /// <summary>
- /// Saves an object to disk in binary format
- /// </summary>
- /// <typeparam name="T">Type of object that will be saved</typeparam>
- /// <param name="data">Object to be saved</param>
- /// <param name="path">Path where object will be saved</param>
- /// <param name="suppress">Flag indicating if exception will be caught if error occurs</param>
- /// <returns>Flag indicating if save was successful or not</returns>
- public static bool SaveBinary<T>(T data, string path, bool suppress = true)
- {
- var binFormatter = new BinaryFormatter();
- try
- {
- using (var stream = File.OpenWrite(path))
- binFormatter.Serialize(stream, data);
- return true;
- }
- catch (Exception error)
- {
- if (suppress)
- return false;
- throw error;
- }
- }
- /// <summary>
- /// Saves an object to disk in XML format
- /// </summary>
- /// <typeparam name="T">Type of object that will be saved</typeparam>
- /// <param name="data">Object to be saved</param>
- /// <param name="path">Path where object will be saved</param>
- /// <param name="suppress">Flag indicating if exception will be caught if error occurs</param>
- /// <returns>Flag indicating if save was successful or not</returns>
- public static bool SaveXML<T>(T data, string path, bool suppress = true)
- {
- try
- {
- var serializer = new XmlSerializer(typeof(T));
- using (TextWriter writerStream = new StreamWriter(path))
- serializer.Serialize(writerStream, data);
- return true;
- }
- catch (Exception error)
- {
- if (suppress)
- return false;
- throw error;
- }
- }
- /// <summary>
- /// Loads a previously binary serialized object and returns it
- /// </summary>
- /// <typeparam name="T">Type of object that will be loaded</typeparam>
- /// <param name="path">Path where object to load from</param>
- /// <param name="suppress">Flag indicating if default instance of object will be returned if exception occurs</param>
- /// <returns>The deserialized object, or new instance of that type if error occured</returns>
- public static T LoadBinary<T>(string path, bool suppress = true)
- {
- var binFormatter = new BinaryFormatter();
- try
- {
- T data;
- using (var stream = File.OpenRead(path))
- data = (T)binFormatter.Deserialize(stream);
- return data;
- }
- catch (Exception error)
- {
- if (suppress)
- return Activator.CreateInstance<T>();
- throw error;
- }
- }
- /// <summary>
- /// Loads a previously XML serialized object and returns it
- /// </summary>
- /// <typeparam name="T">Type of object that will be loaded</typeparam>
- /// <param name="path">Path where object to load from</param>
- /// <param name="suppress">Flag indicating if default instance of object will be returned if exception occurs</param>
- /// <returns>The deserialized object, or new instance of that type if error occured</returns>
- public static T LoadXML<T>(string path, bool suppress = true)
- {
- try
- {
- var serializer = new XmlSerializer(typeof(T));
- using (TextReader readerStream = new StreamReader(path))
- return (T)serializer.Deserialize(readerStream);
- }
- catch (Exception error)
- {
- if (suppress)
- return Activator.CreateInstance<T>();
- throw error;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement