/// <summary> /// Serialize to text writer. /// </summary> /// <param name="writer"></param> /// <param name="p"></param> /// <returns></returns> public static Result SerializeXml(TextWriter writer, object p) { try { XmlSerializer serializer = new XmlSerializer(p.GetType()); serializer.Serialize(writer, p); } catch (Exception ex) { CoreSystemMonitor.Error("Failed to xml-serialize object [" + p.GetType().Name + "]", ex); return(Result.Fail(ex)); } return(Result.Success); }
/// <summary> /// Perform deserialization of an object from a stream. /// </summary> public static Result Deserialize(MemoryStream stream, out object value) { try { IFormatter formatter = ObtainFormatter(); value = formatter.Deserialize(stream); } catch (Exception ex) { CoreSystemMonitor.Error("Failed to deserialize object.", ex); value = null; return(Result.Fail(ex)); } return(Result.Success); }
public static Result DeSerializeXml <TType>(TextReader reader, out TType output) where TType : class { output = null; try { XmlSerializer serializer = new XmlSerializer(typeof(TType)); output = (TType)serializer.Deserialize(reader); } catch (Exception ex) { CoreSystemMonitor.Error("Failed to xml-deserialize object [" + typeof(TType).Name + "]", ex); return(Result.Fail(ex)); } return(Result.Success); }
/// <summary> /// Serialize to memory stream. /// </summary> /// <param name="stream"></param> /// <param name="p"></param> /// <returns></returns> public static Result Serialize(MemoryStream stream, object p) { try { IFormatter formatter = ObtainFormatter(); formatter.Serialize(stream, p); if (stream.Position > SerializationWarningLimit) { CoreSystemMonitor.Warning("Serialialization of object [" + p.GetType().Name + "] has grown above the default serialization limit to [" + stream.Position.ToString() + "] bytes."); } return(Result.Success); } catch (Exception ex) { CoreSystemMonitor.Error("Failed to serialize object [" + p.GetType().Name + "," + ex.Message + "]."); return(Result.Fail(ex)); } }
/// <summary> /// Static constructor - create the default formatter. /// </summary> static SerializationHelper() { lock (_syncRoot) { if (_formatter != null) { CoreSystemMonitor.Error("Not expected, formatter already created."); return; } // Create the formatter. _formatter = new BinaryFormatter(); // 2. Construct a SurrogateSelector object SurrogateSelector surrogateSelector = new SurrogateSelector(); // 3. Tell the surrogate selector to use our object when a // object is serialized/deserialized. List <ISerializationSurrogate> surrogates = ReflectionHelper.GetTypeChildrenInstances <ISerializationSurrogate>( ReflectionHelper.GetAssemblies(true, true)); foreach (ISerializationSurrogate surrogate in surrogates) { SerializationSurrogateAttribute attribute = ReflectionHelper.GetTypeCustomAttributeInstance <SerializationSurrogateAttribute>( surrogate.GetType(), false); if (attribute != null) { surrogateSelector.AddSurrogate(attribute.Type, new StreamingContext(StreamingContextStates.All), surrogate); } else { CoreSystemMonitor.Info(string.Format("Surrogate type [{0}] not marked with SerializationSurrogateAttribute.", surrogate.GetType().Name)); } } _formatter.SurrogateSelector = surrogateSelector; } }