/// <summary> /// Deeps the clone. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="self">The self.</param> /// <param name="obj">The object.</param> /// <returns>T.</returns> public static T DeepClone <T>(this ObjectSerializerBase self, T obj) { using (MemoryStream ms = new MemoryStream()) { self.Serialize(obj, ms); ms.Seek(0, SeekOrigin.Begin); return(self.Deserialize <T>(ms)); } }
/// <summary> /// Sets the default. /// </summary> /// <param name="def">The definition.</param> /// <returns>ObjectSerializerBase.</returns> public static ObjectSerializerBase SetDefault(ObjectSerializerBase def) { ObjectSerializerBase oldValue, currentValue; currentValue = _default; do { oldValue = currentValue; currentValue = Interlocked.CompareExchange(ref _default, def, oldValue); }while (currentValue != oldValue); return(_default); }
/// <summary> /// Initializes static members of the <see cref="SerializerFactory"/> class. /// </summary> static SerializerFactory() { _objectSerializerCache = new Dictionary <string, ObjectSerializerBase>(StringComparer.OrdinalIgnoreCase); #if NETFULL || NETSTANDARD2_0 _objectSerializerCache["binary"] = BinarySerializer.Instance; _objectSerializerCache["DataContract"] = DataContractSerializer.Instance; _objectSerializerCache["dc"] = DataContractSerializer.Instance; #endif #if NETFULL _objectSerializerCache["soap"] = SoapSerializer.Instance; #endif _objectSerializerCache["xml"] = XmlSerializer.Instance; _default = XmlSerializer.Instance; }
/// <summary> /// Deserializes from string. /// </summary> /// <typeparam name="To">The type of to.</typeparam> /// <param name="self">The self.</param> /// <param name="serializedText">The serialized text.</param> /// <returns>To.</returns> public static To DeserializeFromString <To>(this ObjectSerializerBase self, string serializedText) { return((To)self.DeserializeFromString(serializedText, typeof(To))); }
/// <summary> /// Deserializes the specified stream. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="self">The self.</param> /// <param name="stream">The stream.</param> /// <returns>T.</returns> public static T Deserialize <T>(this ObjectSerializerBase self, Stream stream) { return((T)self.Deserialize(stream, typeof(T))); }