/// <summary> /// Deserializes an object from a string using a formatter /// </summary> /// <param name="buffer">the string containing the serialized object</param> /// <param name="type">the type of formatter to use</param> /// <returns>the deserialized object from the string</returns> public static object Deserialize(string buffer, FormatterTypes type) { object obj = null; try { // select the desired formatter (ie. Soap or Binary) IFormatter formatter = GetFormatter(type); // formatter.Binder = new SnapInsSerializationBinder(null); byte[] bytes = System.Text.Encoding.ASCII.GetBytes(buffer); MemoryStream stream = new MemoryStream(bytes); obj = formatter.Deserialize(stream); stream.Close(); return(obj); } catch (System.Exception systemException) { System.Diagnostics.Trace.WriteLine(systemException); if (_throwExceptions) { throw(systemException); } } return(obj); }
// public static object Deserialize(FormatterTypes formatterType, byte[] buffer, System.Runtime.Serialization.SerializationBinder binder) // { // object obj = null; // try // { // // select the desired formatter (ie. Soap or Binary) // IFormatter formatter = GetFormatter(formatterType); // //// formatter.Binder = binder; // // Stream stream = new MemoryStream(buffer); // // obj = formatter.Deserialize(stream); // // stream.Close(); // } // catch(System.Exception systemException) // { // System.Diagnostics.Trace.WriteLine(systemException); // // if (_throwExceptions) // throw(systemException); // } // return obj; // } /// <summary> /// Deserializes an object from a file using a formatter /// </summary> /// <param name="type">the type of formatter to use</param> /// <param name="filename">the file to load the serialized object from</param> /// <returns>the deserialized object from the file</returns> public static object Deserialize(FormatterTypes type, string filename) { object obj = null; try { // select the desired formatter (ie. Soap or Binary) IFormatter formatter = GetFormatter(type); // formatter.Binder = new SnapInsSerializationBinder(null); Stream stream = File.Open(filename, FileMode.Open); obj = formatter.Deserialize(stream); stream.Close(); } catch (System.Exception systemException) { System.Diagnostics.Trace.WriteLine(systemException); if (_throwExceptions) { throw(systemException); } } return(obj); }
/// <summary> /// Serializes an object to a string using a formatter /// </summary> /// <param name="sender">the object to serialize</param> /// <param name="type">the type of formatter to use</param> /// <returns>a string containing the serialized object</returns> public static string Serialize(object sender, FormatterTypes type) { string buffer = null; try { // select the desired formatter (ie. Soap or Binary) IFormatter formatter = GetFormatter(type); MemoryStream stream = new MemoryStream(); formatter.Serialize(stream, sender); buffer = System.Text.Encoding.ASCII.GetString(stream.GetBuffer()); stream.Close(); } catch (System.Exception systemException) { System.Diagnostics.Trace.WriteLine(systemException); if (_throwExceptions) { throw(systemException); } } return(buffer); }
/// <summary> /// Either adds a new or update an existing type in the <i>FormatterTypes</i> collection /// </summary> /// <param name="formatterName"> /// A string that specifies the name that must be associated with this new formatter /// </param> /// <param name="parameters"> /// A string that holds a name-value pairs expression which will be passed /// to the actual LogFormatter instance during its initialization /// </param> /// <param name="isDefaultFormatter"> /// A bool that specifies if the registered formatter should also be set a default formatter /// </param> /// <exception cref="InvalidOperationException"> /// Thrown when the specified type does not implement the <i>ILogFormatter</i> /// interface or has no parameterless constructor /// </exception> public virtual void RegisterFormatterType <Tformatter>( string formatterName, string parameters, bool isDefaultFormatter) where Tformatter : ILogFormatter <T> { lock (FormatterTypes) { // Check if we need to update exiting or add new entry LogConfigFormatterType <T> formatterType; if (TryGetFormatterType(formatterName, out formatterType)) { // Update existing formatterType.Name = formatterName; formatterType.Parameters = parameters; formatterType.FormatterType = typeof(Tformatter); } else { // Add new formatter FormatterTypes.Add(new LogConfigFormatterType <T>( formatterName, typeof(Tformatter), parameters)); } // Do we set formatter as new default formatter? if (isDefaultFormatter) { // Create also new default formatter DefaultFormatter = new LogConfigFormatterType <T>( formatterName, typeof(Tformatter), parameters); } else { // Check if default formatter also matches if (DefaultFormatter != null && StringComparer.InvariantCultureIgnoreCase.Equals(formatterName, DefaultFormatter.Name)) { // Update default formatter DefaultFormatter.Name = formatterName; DefaultFormatter.Parameters = parameters; DefaultFormatter.FormatterType = typeof(Tformatter); } } } }
/// <summary> /// Gets a new formatter of the specified type /// </summary> /// <param name="type">the type of formatter to instanciate</param> /// <returns>an instanciated formatter object</returns> private static IFormatter GetFormatter(FormatterTypes type) { if (type == FormatterTypes.Binary) { return(new BinaryFormatter()); } if (type == FormatterTypes.Soap) { return(new SoapFormatter()); } // default so we don't have failure return(new SoapFormatter()); }
public static void Serialize(object sender, FormatterTypes type, Stream stream) { try { // select the desired formatter (ie. Soap or Binary) IFormatter formatter = GetFormatter(type); formatter.Serialize(stream, sender); } catch(System.Exception systemException) { System.Diagnostics.Trace.WriteLine(systemException); if (_throwExceptions) throw(systemException); } }
public static void Serialize(object sender, FormatterTypes type, Stream stream) { try { // select the desired formatter (ie. Soap or Binary) IFormatter formatter = GetFormatter(type); formatter.Serialize(stream, sender); } catch (System.Exception systemException) { System.Diagnostics.Trace.WriteLine(systemException); if (_throwExceptions) { throw(systemException); } } }
/// <summary> /// Serializes an object to a file using a formatter /// </summary> /// <param name="sender">the object to serialize</param> /// <param name="type">the type of formatter to use</param> /// <param name="filename">the file to write to</param> public static void Serialize(object sender, FormatterTypes type, string filename) { try { // select the desired formatter (ie. Soap or Binary) IFormatter formatter = GetFormatter(type); Stream stream = File.Open(filename, FileMode.Create); formatter.Serialize(stream, sender); stream.Close(); } catch (System.Exception systemException) { System.Diagnostics.Trace.WriteLine(systemException); if (_throwExceptions) { throw(systemException); } } }
/// <summary> /// Serializes an object to a file using a formatter /// </summary> /// <param name="sender">the object to serialize</param> /// <param name="type">the type of formatter to use</param> /// <param name="filename">the file to write to</param> public static void Serialize(object sender, FormatterTypes type, string filename) { try { // select the desired formatter (ie. Soap or Binary) IFormatter formatter = GetFormatter(type); Stream stream = File.Open(filename, FileMode.Create); formatter.Serialize(stream, sender); stream.Close(); } catch(System.Exception systemException) { System.Diagnostics.Trace.WriteLine(systemException); if (_throwExceptions) throw(systemException); } }
/// <summary> /// Gets a new formatter of the specified type /// </summary> /// <param name="type">the type of formatter to instanciate</param> /// <returns>an instanciated formatter object</returns> private static IFormatter GetFormatter(FormatterTypes type) { if (type == FormatterTypes.Binary) return new BinaryFormatter(); if (type == FormatterTypes.Soap) return new SoapFormatter(); // default so we don't have failure return new SoapFormatter(); }
/// <summary> /// Deserializes an object from a string using a formatter /// </summary> /// <param name="buffer">the string containing the serialized object</param> /// <param name="type">the type of formatter to use</param> /// <returns>the deserialized object from the string</returns> public static object Deserialize(string buffer, FormatterTypes type) { object obj = null; try { // select the desired formatter (ie. Soap or Binary) IFormatter formatter = GetFormatter(type); // formatter.Binder = new SnapInsSerializationBinder(null); byte[] bytes = System.Text.Encoding.ASCII.GetBytes(buffer); MemoryStream stream = new MemoryStream(bytes); obj = formatter.Deserialize(stream); stream.Close(); return obj; } catch(System.Exception systemException) { System.Diagnostics.Trace.WriteLine(systemException); if (_throwExceptions) throw(systemException); } return obj; }
// public static object Deserialize(FormatterTypes formatterType, byte[] buffer, System.Runtime.Serialization.SerializationBinder binder) // { // object obj = null; // try // { // // select the desired formatter (ie. Soap or Binary) // IFormatter formatter = GetFormatter(formatterType); // //// formatter.Binder = binder; // // Stream stream = new MemoryStream(buffer); // // obj = formatter.Deserialize(stream); // // stream.Close(); // } // catch(System.Exception systemException) // { // System.Diagnostics.Trace.WriteLine(systemException); // // if (_throwExceptions) // throw(systemException); // } // return obj; // } /// <summary> /// Deserializes an object from a file using a formatter /// </summary> /// <param name="type">the type of formatter to use</param> /// <param name="filename">the file to load the serialized object from</param> /// <returns>the deserialized object from the file</returns> public static object Deserialize(FormatterTypes type, string filename) { object obj = null; try { // select the desired formatter (ie. Soap or Binary) IFormatter formatter = GetFormatter(type); // formatter.Binder = new SnapInsSerializationBinder(null); Stream stream = File.Open(filename, FileMode.Open); obj = formatter.Deserialize(stream); stream.Close(); } catch(System.Exception systemException) { System.Diagnostics.Trace.WriteLine(systemException); if (_throwExceptions) throw(systemException); } return obj; }
/// <summary> /// Serializes an object to a string using a formatter /// </summary> /// <param name="sender">the object to serialize</param> /// <param name="type">the type of formatter to use</param> /// <returns>a string containing the serialized object</returns> public static string Serialize(object sender, FormatterTypes type) { string buffer = null; try { // select the desired formatter (ie. Soap or Binary) IFormatter formatter = GetFormatter(type); MemoryStream stream = new MemoryStream(); formatter.Serialize(stream, sender); buffer = System.Text.Encoding.ASCII.GetString(stream.GetBuffer()); stream.Close(); } catch(System.Exception systemException) { System.Diagnostics.Trace.WriteLine(systemException); if (_throwExceptions) throw(systemException); } return buffer; }